dev-think-one /
laravel-periodic-notice
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PeriodicNotice\Console\Commands; |
||
| 4 | |||
| 5 | use Illuminate\Console\Command; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
| 8 | use PeriodicNotice\Contracts\NotificationReceiver; |
||
| 9 | |||
| 10 | class SendPeriodicalNotificationsBatch extends Command |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The name and signature of the console command. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $signature = 'periodic-notice:send:batch |
||
| 18 | {type : Period type} |
||
| 19 | {receiver : Receiver class} |
||
| 20 | {--G|group=default : Group of notifications} |
||
| 21 | '; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The console command description. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $description = 'Send periodic notice to receivers by period type.'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Execute the console command. |
||
| 32 | * |
||
| 33 | * @return int |
||
| 34 | * |
||
| 35 | */ |
||
| 36 | 3 | public function handle() |
|
| 37 | { |
||
| 38 | 3 | $type = $this->argument('type'); |
|
| 39 | /** |
||
| 40 | * @var class-string<Model> $receiverClass |
||
| 41 | */ |
||
| 42 | 3 | $receiverClass = $this->argument('receiver'); |
|
| 43 | 3 | $group = $this->option('group'); |
|
| 44 | |||
| 45 | 3 | if (!$this->isValidReceiverClass($receiverClass)) { |
|
| 46 | 1 | $originalReceiverClass = $receiverClass; |
|
| 47 | 1 | $receiverClass = Relation::getMorphedModel($receiverClass); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 48 | 1 | if (!$this->isValidReceiverClass($receiverClass)) { |
|
| 49 | 1 | $this->error("Please specify correct receiver. Currently specified: [{$originalReceiverClass}]"); |
|
| 50 | |||
| 51 | 1 | return 1; |
|
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \PeriodicNotice\Contracts\NotificationReceiver $receiverObject |
||
| 57 | * @psalm-suppress UndefinedClass |
||
| 58 | */ |
||
| 59 | 2 | $receiverObject = new $receiverClass(); |
|
| 60 | 2 | $periodicNoticeDirector = $receiverObject->periodicNoticeDirector($group); |
|
| 61 | |||
| 62 | 2 | $allowedPeriodTypes = $periodicNoticeDirector->allowedPeriodTypes(); |
|
| 63 | |||
| 64 | 2 | if (empty($type) |
|
| 65 | 2 | || !in_array($type, $allowedPeriodTypes) |
|
| 66 | ) { |
||
| 67 | 1 | $allowedPeriodTypesString = implode(', ', $allowedPeriodTypes); |
|
| 68 | 1 | $this->error("Please specify correct period type. Currently specified: [{$type}]. Allowed types: [{$allowedPeriodTypesString}]"); |
|
| 69 | |||
| 70 | 1 | return 2; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** @psalm-suppress UndefinedClass */ |
||
| 74 | 1 | $receiverClass::query()->withNotificationPeriodType($type) |
|
| 75 | 1 | ->chunk(200, function ($receivers) use ($group) { |
|
| 76 | /** @var \PeriodicNotice\Contracts\NotificationReceiver $receiver */ |
||
| 77 | 1 | foreach ($receivers as $receiver) { |
|
| 78 | 1 | $receiver->sendPeriodicalNotification($group); |
|
| 79 | } |
||
| 80 | 1 | }); |
|
| 81 | |||
| 82 | 1 | return 0; |
|
| 83 | } |
||
| 84 | |||
| 85 | 3 | protected function isValidReceiverClass($receiverClass): bool |
|
| 86 | { |
||
| 87 | 3 | return is_string($receiverClass) |
|
| 88 | 3 | && is_a($receiverClass, NotificationReceiver::class, true) |
|
| 89 | 3 | && is_a($receiverClass, Model::class, true); |
|
| 90 | } |
||
| 91 | } |
||
| 92 |