Skip to content

Commit 18346c0

Browse files
Handle $deleteWhenMissingModels on JobDecorator (#283)
1 parent d5c2ca5 commit 18346c0

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/Decorators/JobDecorator.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class JobDecorator implements ShouldQueue
3333
protected string $actionClass;
3434
protected array $parameters = [];
3535

36+
public ?bool $deleteWhenMissingModels;
37+
3638
public function __construct(string $action, ...$parameters)
3739
{
3840
$this->actionClass = $action;
@@ -48,6 +50,7 @@ protected function constructed()
4850
$this->setTries($this->fromActionProperty('jobTries'));
4951
$this->setMaxExceptions($this->fromActionProperty('jobMaxExceptions'));
5052
$this->setTimeout($this->fromActionProperty('jobTimeout'));
53+
$this->setDeleteWhenMissingModels($this->fromActionProperty('jobDeleteWhenMissingModels'));
5154
$this->fromActionMethod('configureJob', [$this]);
5255
}
5356

@@ -105,6 +108,13 @@ public function setTimeout(?int $timeout)
105108
return $this;
106109
}
107110

111+
public function setDeleteWhenMissingModels(?bool $deleteWhenMissingModels)
112+
{
113+
$this->deleteWhenMissingModels = $deleteWhenMissingModels;
114+
115+
return $this;
116+
}
117+
108118
public function decorates(string $actionClass): bool
109119
{
110120
return $this->getAction() instanceof $actionClass;
@@ -180,11 +190,12 @@ protected function getPrependedParameters(string $method): array
180190

181191
if ($firstParameter->allowsNull() && $firstParameterClass === Batch::class) {
182192
return [$this->batch(), ...$this->parameters];
183-
} elseif (is_subclass_of($firstParameterClass, self::class) || $firstParameterClass === self::class) {
193+
}
194+
if (is_subclass_of($firstParameterClass, self::class) || $firstParameterClass === self::class) {
184195
return [$this, ...$this->parameters];
185-
} else {
186-
return $this->parameters;
187196
}
197+
198+
return $this->parameters;
188199
}
189200

190201
protected function serializeProperties()

tests/AsJobConfiguredWithMethodsTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public function configureJob(JobDecorator $job)
1818
->onQueue('my_queue')
1919
->through(['my_middleware'])
2020
->chain(['my_chain'])
21-
->delay(60);
21+
->delay(60)
22+
->setDeleteWhenMissingModels(true);
2223
}
2324

2425
public function getJobBackoff(): array
@@ -56,6 +57,7 @@ public function handle()
5657
&& $job->middleware === ['my_middleware']
5758
&& $job->chained === [serialize('my_chain')]
5859
&& $job->delay === 60
60+
&& $job->deleteWhenMissingModels === true
5961
&& $job->backoff() === [30, 60, 120]
6062
&& $job->retryUntil()->timestamp === now()->addMinutes(30)->timestamp;
6163
});

tests/AsJobConfiguredWithPropertiesTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AsJobConfiguredWithPropertiesTest
1717
public int $jobBackoff = 60 * 5;
1818
public int $jobTimeout = 60 * 30;
1919
public int $jobRetryUntil = 3600 * 2;
20+
public bool $jobDeleteWhenMissingModels = true;
2021

2122
public function handle()
2223
{
@@ -41,6 +42,7 @@ public function handle()
4142
&& $job->maxExceptions === 3
4243
&& $job->backoff() === 60 * 5
4344
&& $job->timeout === 60 * 30
45+
&& $job->deleteWhenMissingModels === true
4446
&& $job->retryUntil() === 3600 * 2;
4547
});
4648
});

0 commit comments

Comments
 (0)