| Total Complexity | 7 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | class JobFactory implements JobFactoryInterface |
||
| 11 | { |
||
| 12 | 5 | public function createFromArray(array $data): JobInterface |
|
| 13 | { |
||
| 14 | 5 | if (!isset($data['class'], $data['data'])) { |
|
| 15 | 1 | throw new InvalidArgumentException('Data does not contain required class key'); |
|
| 16 | } |
||
| 17 | |||
| 18 | 4 | if (!class_exists($data['class'])) { |
|
| 19 | 1 | throw new InvalidArgumentException("Unknown class '{$data['class']}' given"); |
|
| 20 | } |
||
| 21 | |||
| 22 | 3 | if (!is_subclass_of($data['class'], JobInterface::class)) { |
|
| 23 | 1 | throw new InvalidArgumentException("Class '{$data['class']}' does not implement JobInterface"); |
|
| 24 | } |
||
| 25 | |||
| 26 | 2 | return $data['class']::fromArray($data['data']); |
|
| 27 | } |
||
| 28 | |||
| 29 | 1 | public function createFromJson(string $data): JobInterface |
|
| 32 | } |
||
| 33 | |||
| 34 | 2 | public function saveToArray(JobInterface $job): array |
|
| 35 | { |
||
| 36 | return [ |
||
| 37 | 2 | 'class' => get_class($job), |
|
| 38 | 2 | 'data' => $job->jsonSerialize(), |
|
| 39 | ]; |
||
| 40 | } |
||
| 41 | |||
| 42 | 1 | public function saveToJson(JobInterface $job): string |
|
| 45 | } |
||
| 46 | } |
||
| 47 |