diff --git a/composer.json b/composer.json index 8520196..071ad4e 100644 --- a/composer.json +++ b/composer.json @@ -22,13 +22,13 @@ }, "autoload": { "psr-4": { - "BiiiiiigMonster\\Clearable\\": "src", - "BiiiiiigMonster\\Clearable\\Database\\Factories\\": "database/factories" + "BiiiiiigMonster\\Clearable\\": "src" } }, "autoload-dev": { "psr-4": { - "BiiiiiigMonster\\Clearable\\Tests\\": "tests" + "BiiiiiigMonster\\Clearable\\Tests\\": "tests", + "BiiiiiigMonster\\Clearable\\Database\\Factories\\": "database/factories" } }, "extra": { diff --git a/src/ClearManager.php b/src/ClearManager.php index 20ffdab..766cc76 100644 --- a/src/ClearManager.php +++ b/src/ClearManager.php @@ -5,7 +5,6 @@ use BiiiiiigMonster\Clearable\Attributes\Clear; use BiiiiiigMonster\Clearable\Jobs\ClearsJob; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Collection; use ReflectionClass; use ReflectionMethod; @@ -44,17 +43,14 @@ public function handle(): void $this->model::class, $this->model->getOriginal(), $relationName, - $relations = Collection::wrap($this->model->$relationName), $clear->invokableClearClassName ]; - if ($relations->isNotEmpty()) { - match ($clear->clearQueue) { - null,false => ClearsJob::dispatchSync(...$payload), - true,'' => ClearsJob::dispatch(...$payload), - default => ClearsJob::dispatch(...$payload)->onQueue($clear->clearQueue) - }; - } + match ($clear->clearQueue) { + null,false => ClearsJob::dispatchSync(...$payload), + true,'' => ClearsJob::dispatch(...$payload), + default => ClearsJob::dispatch(...$payload)->onQueue($clear->clearQueue) + }; } } diff --git a/src/Concerns/HasClears.php b/src/Concerns/HasClears.php index b427839..8c36e5a 100644 --- a/src/Concerns/HasClears.php +++ b/src/Concerns/HasClears.php @@ -10,7 +10,6 @@ * * @property array $clears The relationships that will be auto-cleared when deleted. * @property string|bool|null $clearQueue The clearable that will be dispatch on this name queue. - * @package BiiiiiigMonster\Clears\Concerns */ trait HasClears { diff --git a/src/Console/InvokableClearMakeCommand.php b/src/Console/InvokableClearMakeCommand.php index b1ca6cd..688e2ae 100644 --- a/src/Console/InvokableClearMakeCommand.php +++ b/src/Console/InvokableClearMakeCommand.php @@ -43,7 +43,7 @@ class InvokableClearMakeCommand extends GeneratorCommand */ protected function getStub() { - $relativePath = '/stubs/invoke-clear.stub'; + $relativePath = '/stubs/invokable-clear.stub'; return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/'))) ? $customPath diff --git a/src/Console/stubs/invoke-clear.stub b/src/Console/stubs/invokable-clear.stub similarity index 87% rename from src/Console/stubs/invoke-clear.stub rename to src/Console/stubs/invokable-clear.stub index 37647c6..aebe3a8 100644 --- a/src/Console/stubs/invoke-clear.stub +++ b/src/Console/stubs/invokable-clear.stub @@ -13,7 +13,7 @@ class {{ class }} implements InvokableClear * @param Model $clear * @return bool */ - public function __invoke($clear): bool + public function __invoke($clear) { // } diff --git a/src/Contracts/InvokableClear.php b/src/Contracts/InvokableClear.php index 6ef74dc..8bab3de 100644 --- a/src/Contracts/InvokableClear.php +++ b/src/Contracts/InvokableClear.php @@ -12,5 +12,5 @@ interface InvokableClear * @param Model $clear * @return bool */ - public function __invoke($clear): bool; + public function __invoke($clear); } diff --git a/src/Jobs/ClearsJob.php b/src/Jobs/ClearsJob.php index 761008f..0f966a5 100644 --- a/src/Jobs/ClearsJob.php +++ b/src/Jobs/ClearsJob.php @@ -2,6 +2,7 @@ namespace BiiiiiigMonster\Clearable\Jobs; +use BiiiiiigMonster\Clearable\Contracts\InvokableClear; use BiiiiiigMonster\Clearable\Exceptions\NotAllowedClearsException; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -28,14 +29,12 @@ class ClearsJob implements ShouldQueue * @param string $className * @param array $original * @param string $relationName - * @param Collection $collection * @param string|null $invokableClearClassName */ public function __construct( protected string $className, protected array $original, protected string $relationName, - protected Collection $collection, protected ?string $invokableClearClassName = null, ) { } @@ -51,10 +50,10 @@ public function handle(): void $relation = $model->{$this->relationName}(); // to be cleared model. - $clears = $this->collection; - if ($this->invokableClearClassName) { + $clears = Collection::wrap($model->{$this->relationName}); + if (is_a($this->invokableClearClassName, InvokableClear::class, true)) { $invoke = new $this->invokableClearClassName(); - $clears = $clears->filter(fn (Model $clear) => $invoke($clear, $model)); + $clears = $clears->filter(fn (Model $clear) => $invoke($clear)); } switch (true) {