Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update #3

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 5 additions & 9 deletions src/ClearManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
};
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Concerns/HasClears.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Console/InvokableClearMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class {{ class }} implements InvokableClear
* @param Model $clear
* @return bool
*/
public function __invoke($clear): bool
public function __invoke($clear)
{
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/InvokableClear.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface InvokableClear
* @param Model $clear
* @return bool
*/
public function __invoke($clear): bool;
public function __invoke($clear);
}
9 changes: 4 additions & 5 deletions src/Jobs/ClearsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}
Expand All @@ -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) {
Expand Down