Skip to content

Commit

Permalink
Merge pull request #3 from biiiiiigmonster/fix/readme
Browse files Browse the repository at this point in the history
update
  • Loading branch information
biiiiiigmonster committed Oct 12, 2022
2 parents 2e6f932 + d54585d commit f720654
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 21 deletions.
6 changes: 3 additions & 3 deletions composer.json
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
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
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
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
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
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
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

0 comments on commit f720654

Please sign in to comment.