mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +08:00
refactor event
This commit is contained in:
@@ -4,10 +4,16 @@ namespace App\Console\Commands;
|
||||
|
||||
use App\Events\NewsCreated;
|
||||
use App\Events\TorrentCreated;
|
||||
use App\Events\TorrentDeleted;
|
||||
use App\Events\TorrentUpdated;
|
||||
use App\Events\UserDestroyed;
|
||||
use App\Events\UserDisabled;
|
||||
use App\Events\UserEnabled;
|
||||
use App\Models\News;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class FireEvent extends Command
|
||||
{
|
||||
@@ -26,11 +32,13 @@ class FireEvent extends Command
|
||||
protected $description = 'Fire a event, options: --name, --id';
|
||||
|
||||
protected array $eventMaps = [
|
||||
"torrent_created" => TorrentCreated::class,
|
||||
"user_destroyed" => UserDestroyed::class,
|
||||
"user_disabled" => UserDisabled::class,
|
||||
"user_enabled" => UserEnabled::class,
|
||||
"news_created" => NewsCreated::class,
|
||||
"torrent_created" => ['event' => TorrentCreated::class, 'model' => Torrent::class],
|
||||
"torrent_updated" => ['event' => TorrentUpdated::class, 'model' => Torrent::class],
|
||||
"torrent_deleted" => ['event' => TorrentDeleted::class, 'model' => Torrent::class],
|
||||
"user_destroyed" => ['event' => UserDestroyed::class, 'model' => User::class],
|
||||
"user_disabled" => ['event' => UserDisabled::class, 'model' => User::class],
|
||||
"user_enabled" => ['event' => UserEnabled::class, 'model' => User::class],
|
||||
"news_created" => ['event' => NewsCreated::class, 'model' => News::class],
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -44,7 +52,11 @@ class FireEvent extends Command
|
||||
$id = $this->option('id');
|
||||
$log = "FireEvent, name: $name, id: $id";
|
||||
if (isset($this->eventMaps[$name])) {
|
||||
$result = call_user_func([$this->eventMaps[$name], "dispatch"], $id);
|
||||
$eventName = $this->eventMaps[$name]['event'];
|
||||
$modelName = $this->eventMaps[$name]['model'];
|
||||
/** @var Model $model */
|
||||
$model = new $modelName();
|
||||
$result = call_user_func([$eventName, "dispatch"], $model::query()->find($id));
|
||||
$this->info("$log, success call dispatch, result: " . var_export($result, true));
|
||||
} else {
|
||||
$this->error("$log, no event match this name");
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class NewsCreated
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $id;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $id)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class TorrentCreated
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $torrentId;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $torrentId)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->torrentId = $torrentId;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class TorrentDeleted
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $torrentId;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $torrentId)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->torrentId = $torrentId;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class TorrentUpdated
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $torrentId;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $torrentId)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->torrentId = $torrentId;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class UserDestroyed
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $id;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $id)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class UserDisabled
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $id;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $id)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
@@ -14,16 +15,16 @@ class UserEnabled
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public int $id;
|
||||
public ?Model $model = null;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(int $id)
|
||||
public function __construct(Model $model)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,7 +27,7 @@ class FetchTorrentImdb implements ShouldQueue
|
||||
*/
|
||||
public function handle($event)
|
||||
{
|
||||
$torrentId = $event->torrentId;
|
||||
$torrentId = $event->model?->id ?? 0;
|
||||
$torrentRep = new TorrentRepository();
|
||||
$torrentRep->fetchImdb($torrentId);
|
||||
do_log("fetchImdb for torrent: $torrentId done!");
|
||||
|
||||
@@ -27,7 +27,7 @@ class RemoveOauthTokens implements ShouldQueue
|
||||
*/
|
||||
public function handle($event)
|
||||
{
|
||||
$uid = $event->id;
|
||||
$uid = $event->model?->id ?? 0;
|
||||
$modelNames = [
|
||||
Passport::$authCodeModel,
|
||||
Passport::$tokenModel,
|
||||
|
||||
@@ -31,10 +31,10 @@ class SyncTorrentToEs implements ShouldQueue
|
||||
*/
|
||||
public function handle($event)
|
||||
{
|
||||
$id = $event->torrentId;
|
||||
$id = $event->model?->id ?? 0;
|
||||
$searchRep = new SearchRepository();
|
||||
$result = $searchRep->updateTorrent($id);
|
||||
do_log("result: " . var_export($result, true));
|
||||
do_log(sprintf("updateTorrent: %s result: %s", $id, var_export($result, true)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user