adrema/app/Lib/JobMiddleware/WithJobState.php

71 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Lib\JobMiddleware;
2023-08-16 01:07:48 +02:00
use App\Lib\Events\JobFailed;
use App\Lib\Events\JobFinished;
use App\Lib\Events\JobStarted;
use Closure;
use Lorisleiva\Actions\Decorators\JobDecorator;
2023-08-16 01:07:48 +02:00
use Throwable;
class WithJobState
{
public JobStarted $beforeMessage;
public JobFinished $afterMessage;
2023-08-16 01:07:48 +02:00
public JobFailed $failedMessage;
private function __construct(public string $channel)
{
}
public static function make(string $channel): self
{
return new self($channel);
}
public function before(string $message): self
{
$this->beforeMessage = JobStarted::on($this->channel)->withMessage($message);
return $this;
}
public function after(string $message): self
{
$this->afterMessage = JobFinished::on($this->channel)->withMessage($message);
return $this;
}
2023-08-16 01:07:48 +02:00
public function failed(string $message): self
{
$this->failedMessage = JobFailed::on($this->channel)->withMessage($message);
return $this;
}
public function shouldReload(): self
{
$this->afterMessage->shouldReload();
2023-08-16 01:07:48 +02:00
$this->failedMessage->shouldReload();
return $this;
}
public function handle(JobDecorator $job, Closure $next): void
{
event($this->beforeMessage);
2023-08-16 01:07:48 +02:00
try {
$next($job);
} catch (Throwable $e) {
event($this->failedMessage);
throw $e;
}
event($this->afterMessage);
}
}