| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Operations; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Operations\ProductOperationInterface; |
| 6 |
use SyncBasalam\Admin\Product\Operations\UpdateProduct; |
| 7 |
use SyncBasalam\Admin\Product\Operations\CreateProduct; |
| 8 |
use SyncBasalam\Admin\Product\Operations\ArchiveProduct; |
| 9 |
use SyncBasalam\Admin\Product\Operations\RestoreProduct; |
| 10 |
|
| 11 |
defined('ABSPATH') || exit; |
| 12 |
|
| 13 |
class ProductOperationFactory |
| 14 |
{ |
| 15 |
private const OPERATIONS = [ |
| 16 |
'update' => UpdateProduct::class, |
| 17 |
'create' => CreateProduct::class, |
| 18 |
'archive' => ArchiveProduct::class, |
| 19 |
'restore' => RestoreProduct::class, |
| 20 |
]; |
| 21 |
|
| 22 |
public static function create(string $type): ProductOperationInterface |
| 23 |
{ |
| 24 |
if (!isset(self::OPERATIONS[$type])) { |
| 25 |
throw new \InvalidArgumentException( |
| 26 |
esc_html(sprintf('Operation type "%s" is not supported. Available types: %s', |
| 27 |
$type, |
| 28 |
implode(', ', array_keys(self::OPERATIONS)) |
| 29 |
)) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
$operationClass = self::OPERATIONS[$type]; |
| 34 |
return syncBasalamContainer()->get($operationClass); |
| 35 |
} |
| 36 |
|
| 37 |
public static function getAvailableOperations(): array |
| 38 |
{ |
| 39 |
return array_keys(self::OPERATIONS); |
| 40 |
} |
| 41 |
|
| 42 |
public static function isSupported(string $type): bool |
| 43 |
{ |
| 44 |
return isset(self::OPERATIONS[$type]); |
| 45 |
} |
| 46 |
} |
| 47 |
|