| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Jobs; |
| 4 |
|
| 5 |
use SyncBasalam\Jobs\Types\BulkUpdateProductsJob; |
| 6 |
use SyncBasalam\Jobs\Types\UpdateAllProductsJob; |
| 7 |
use SyncBasalam\Jobs\Types\UpdateSingleProductJob; |
| 8 |
use SyncBasalam\Jobs\Types\CreateSingleProductJob; |
| 9 |
use SyncBasalam\Jobs\Types\CreateAllProductsJob; |
| 10 |
use SyncBasalam\Jobs\Types\AutoConnectProductsJob; |
| 11 |
use SyncBasalam\Jobs\Types\FetchOrdersJob; |
| 12 |
|
| 13 |
defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
class JobRegistry |
| 16 |
{ |
| 17 |
private $jobTypes = []; |
| 18 |
|
| 19 |
public function __construct(array $jobTypes = []) |
| 20 |
{ |
| 21 |
if (empty($jobTypes)) { |
| 22 |
$this->registerDefaultJobs(); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
foreach ($jobTypes as $jobType) { |
| 27 |
if ($jobType instanceof JobType) { |
| 28 |
$this->register($jobType); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
private function registerDefaultJobs(): void |
| 34 |
{ |
| 35 |
$container = syncBasalamContainer(); |
| 36 |
$this->register($container->get(BulkUpdateProductsJob::class)); |
| 37 |
$this->register($container->get(UpdateAllProductsJob::class)); |
| 38 |
$this->register($container->get(UpdateSingleProductJob::class)); |
| 39 |
$this->register($container->get(CreateSingleProductJob::class)); |
| 40 |
$this->register($container->get(CreateAllProductsJob::class)); |
| 41 |
$this->register($container->get(AutoConnectProductsJob::class)); |
| 42 |
$this->register($container->get(FetchOrdersJob::class)); |
| 43 |
} |
| 44 |
|
| 45 |
public function register(JobType $jobType): void |
| 46 |
{ |
| 47 |
$this->jobTypes[$jobType->getType()] = $jobType; |
| 48 |
} |
| 49 |
|
| 50 |
public function get(string $type): ?JobType |
| 51 |
{ |
| 52 |
return $this->jobTypes[$type] ?? null; |
| 53 |
} |
| 54 |
|
| 55 |
public function getAll(): array |
| 56 |
{ |
| 57 |
return $this->jobTypes; |
| 58 |
} |
| 59 |
|
| 60 |
public function getSortedByPriority(): array |
| 61 |
{ |
| 62 |
$jobTypes = $this->jobTypes; |
| 63 |
uasort($jobTypes, function (JobType $a, JobType $b) { |
| 64 |
return $a->getPriority() <=> $b->getPriority(); |
| 65 |
}); |
| 66 |
return $jobTypes; |
| 67 |
} |
| 68 |
} |
| 69 |
|