| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Jobs; |
| 4 |
|
| 5 |
use SyncBasalam\Jobs\Exceptions\JobException; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class JobResult |
| 10 |
{ |
| 11 |
private $success; |
| 12 |
private $exception; |
| 13 |
private $data; |
| 14 |
|
| 15 |
private function __construct(bool $success, $exception = null, array $data = []) |
| 16 |
{ |
| 17 |
$this->success = $success; |
| 18 |
$this->exception = $exception; |
| 19 |
$this->data = $data; |
| 20 |
} |
| 21 |
|
| 22 |
public static function success(array $data = []): self |
| 23 |
{ |
| 24 |
return new self(true, null, $data); |
| 25 |
} |
| 26 |
|
| 27 |
public static function failed(JobException $exception, array $data = []): self |
| 28 |
{ |
| 29 |
return new self(false, $exception, $data); |
| 30 |
} |
| 31 |
|
| 32 |
public function isSuccessful(): bool |
| 33 |
{ |
| 34 |
return $this->success; |
| 35 |
} |
| 36 |
|
| 37 |
public function shouldRetry(): bool |
| 38 |
{ |
| 39 |
return !$this->success && $this->exception && $this->exception->shouldRetry(); |
| 40 |
} |
| 41 |
|
| 42 |
public function getException(): ?JobException |
| 43 |
{ |
| 44 |
return $this->exception; |
| 45 |
} |
| 46 |
|
| 47 |
public function getData(): array |
| 48 |
{ |
| 49 |
return $this->data; |
| 50 |
} |
| 51 |
|
| 52 |
public function getErrorMessage(): ?string |
| 53 |
{ |
| 54 |
return $this->exception ? $this->exception->getMessage() : null; |
| 55 |
} |
| 56 |
|
| 57 |
public function getErrorContext(): array |
| 58 |
{ |
| 59 |
return $this->exception ? $this->exception->getErrorContext() : []; |
| 60 |
} |
| 61 |
} |
| 62 |
|