|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tapp\FilamentLibrary\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Tapp\FilamentLibrary\FilamentLibraryPlugin; |
| 8 | +use Tapp\FilamentLibrary\Models\LibraryItem; |
| 9 | + |
| 10 | +class ReviewLibraryItemCommand extends Command |
| 11 | +{ |
| 12 | + public $signature = 'filament-library:review |
| 13 | + {id : Library item id} |
| 14 | + {action : publish, reject, or return} |
| 15 | + {--reason= : Note stored when rejecting} |
| 16 | + {--user= : Gatekeeper user id}'; |
| 17 | + |
| 18 | + public $description = 'Publish, reject, or return a library item'; |
| 19 | + |
| 20 | + public function handle(): int |
| 21 | + { |
| 22 | + $model = FilamentLibraryPlugin::libraryItemModelClass(); |
| 23 | + $item = $model::query()->find($this->argument('id')); |
| 24 | + |
| 25 | + if (! $item instanceof LibraryItem) { |
| 26 | + $this->error('Library item was not found.'); |
| 27 | + |
| 28 | + return self::FAILURE; |
| 29 | + } |
| 30 | + |
| 31 | + $gatekeeperId = $this->gatekeeperId(); |
| 32 | + |
| 33 | + if ($gatekeeperId === false) { |
| 34 | + return self::FAILURE; |
| 35 | + } |
| 36 | + |
| 37 | + $action = strtolower((string) $this->argument('action')); |
| 38 | + $reason = $this->option('reason'); |
| 39 | + $reason = is_string($reason) && trim($reason) !== '' ? trim($reason) : null; |
| 40 | + |
| 41 | + match ($action) { |
| 42 | + 'publish' => $item->publish($gatekeeperId), |
| 43 | + 'reject' => $item->reject($gatekeeperId, $reason), |
| 44 | + 'return' => $item->returnToDraft($gatekeeperId), |
| 45 | + default => null, |
| 46 | + }; |
| 47 | + |
| 48 | + if (! in_array($action, ['publish', 'reject', 'return'], true)) { |
| 49 | + $this->error('Action must be publish, reject, or return.'); |
| 50 | + |
| 51 | + return self::FAILURE; |
| 52 | + } |
| 53 | + |
| 54 | + $item->refresh(); |
| 55 | + $this->info("Library item {$item->getKey()} is now {$item->publication_status->value}."); |
| 56 | + |
| 57 | + return self::SUCCESS; |
| 58 | + } |
| 59 | + |
| 60 | + protected function gatekeeperId(): int | false | null |
| 61 | + { |
| 62 | + $userId = $this->option('user'); |
| 63 | + |
| 64 | + if ($userId === null || $userId === '') { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + $userId = (int) $userId; |
| 69 | + |
| 70 | + if ($userId < 1 || ! DB::table('users')->where('id', $userId)->exists()) { |
| 71 | + $this->error('Gatekeeper user was not found.'); |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + return $userId; |
| 77 | + } |
| 78 | +} |
0 commit comments