Skip to content

Commit

Permalink
Merge pull request #4 from biiiiiigmonster/fix/readme
Browse files Browse the repository at this point in the history
update
  • Loading branch information
biiiiiigmonster authored Oct 14, 2022
2 parents f720654 + 737e8bb commit 80936df
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
7 changes: 5 additions & 2 deletions src/Attributes/Clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BiiiiiigMonster\Clearable\Attributes;

use Attribute;
use BiiiiiigMonster\Clearable\ClearManager;

#[Attribute(Attribute::TARGET_METHOD)]
class Clear
Expand All @@ -11,11 +12,13 @@ class Clear
* Clear constructor.
*
* @param string|null $invokableClearClassName
* @param string|bool|null $clearQueue
* @param string|null $clearQueue
* @param ?string $clearConnection
*/
public function __construct(
public ?string $invokableClearClassName = null,
public string|bool|null $clearQueue = null,
public ?string $clearQueue = null,
public ?string $clearConnection = ClearManager::SYNC_QUEUE_CONNECTION,
) {
}
}
26 changes: 9 additions & 17 deletions src/ClearManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class ClearManager
{
public const SYNC_QUEUE_CONNECTION = 'sync';

/**
* ClearManager constructor.
*
Expand All @@ -36,22 +38,12 @@ public static function make(Model $model): static
*/
public function handle(): void
{
$clears = $this->parse();

foreach ($clears as $relationName => $clear) {
$payload = [
$this->model::class,
$this->model->getOriginal(),
$relationName,
$clear->invokableClearClassName
];

match ($clear->clearQueue) {
null,false => ClearsJob::dispatchSync(...$payload),
true,'' => ClearsJob::dispatch(...$payload),
default => ClearsJob::dispatch(...$payload)->onQueue($clear->clearQueue)
};
}
collect($this->parse())->map(
fn (Clear $clear, string $relationName) =>
ClearsJob::dispatch($this->model->withoutRelations(), $relationName, $clear->invokableClearClassName)
->onConnection($clear->clearConnection)
->onQueue($clear->clearQueue)
);
}

/**
Expand All @@ -70,7 +62,7 @@ protected function parse(): array
$invokableClearClassName = null;
}

$clears[$relationName] = new Clear($invokableClearClassName, $this->model->getClearQueue());
$clears[$relationName] = new Clear($invokableClearClassName, $this->model->getClearQueue(), $this->model->getClearConnection());
}

// from clear attribute
Expand Down
36 changes: 31 additions & 5 deletions src/Concerns/HasClears.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* Trait HasClears
*
* @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.
* @property ?string $clearQueue The clearable that will be dispatch on this named queue.
* @property ?string $clearConnection The clearable that will be dispatch on this connection queue.
*/
trait HasClears
{
Expand Down Expand Up @@ -63,23 +64,48 @@ public function clear(array|string|null $clears): static
/**
* Get clearQueue.
*
* @return string|bool|null
* @return string|null
*/
public function getClearQueue(): string|bool|null
public function getClearQueue(): ?string
{
return $this->clearQueue;
}

/**
* Set the clearQueue attributes for the model.
*
* @param string|bool|null $clearQueue
* @param string|null $clearQueue
* @return $this
*/
public function setClearQueue(string|bool|null $clearQueue): static
public function setClearQueue(?string $clearQueue): static
{
$this->clearQueue = $clearQueue;

return $this;
}

/**
* Get clearConnection.
*
* @return string|null
*/
public function getClearConnection(): ?string
{
return property_exists(static::class, 'clearConnection')
? $this->clearConnection
: ClearManager::SYNC_QUEUE_CONNECTION;
}

/**
* Set the clearConnection attributes for the model.
*
* @param string $clearConnection
* @return $this
*/
public function setClearConnection(string $clearConnection): static
{
$this->clearConnection = $clearConnection;

return $this;
}
}
21 changes: 7 additions & 14 deletions src/Jobs/ClearsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,25 @@
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use LogicException;

class ClearsJob implements ShouldQueue
{
use Dispatchable;
use Queueable;
use SerializesModels;
use InteractsWithQueue;

/**
* ClearJob constructor.
*
* @param string $className
* @param array $original
* @param Model $model
* @param string $relationName
* @param string|null $invokableClearClassName
*/
public function __construct(
protected string $className,
protected array $original,
protected Model $model,
protected string $relationName,
protected ?string $invokableClearClassName = null,
protected ?string $invokableClearClassName,
) {
}

Expand All @@ -46,11 +40,10 @@ public function __construct(
*/
public function handle(): void
{
$model = (new $this->className())->setRawAttributes($this->original);
$relation = $model->{$this->relationName}();
$relation = $this->model->{$this->relationName}();

// to be cleared model.
$clears = Collection::wrap($model->{$this->relationName});
$clears = Collection::wrap($this->model->{$this->relationName});
if (is_a($this->invokableClearClassName, InvokableClear::class, true)) {
$invoke = new $this->invokableClearClassName();
$clears = $clears->filter(fn (Model $clear) => $invoke($clear));
Expand All @@ -66,14 +59,14 @@ public function handle(): void
case $relation instanceof BelongsTo:
throw new NotAllowedClearsException(sprintf(
'%s::%s is relationship of %s, it not allowed to be cleared.',
$this->className,
$this->model::class,
$this->relationName,
$relation::class
));
default:
throw new LogicException(sprintf(
'%s::%s must return a relationship instance.',
$this->className,
$this->model::class,
$this->relationName
));
}
Expand Down
9 changes: 0 additions & 9 deletions tests/Features/ClearQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
use BiiiiiigMonster\Clearable\Tests\Models\User;
use Illuminate\Support\Facades\Queue;

test('Clear use queue test', function () {
Queue::fake();

$user = User::has('posts', '>=', 2)->with('posts')->first();
$user->clear('posts')->setClearQueue(true)->delete();

Queue::assertPushed(ClearsJob::class);
});

test('Clear named queue test', function () {
Queue::fake();

Expand Down

0 comments on commit 80936df

Please sign in to comment.