-
Notifications
You must be signed in to change notification settings - Fork 9
/
Manager.php
82 lines (70 loc) · 2.05 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Jh\Import\Import;
use InvalidArgumentException;
use Jh\Import\Config\Data;
use Jh\Import\Type\Db;
use Jh\Import\Type\Files;
use Jh\Import\Type\Type;
use Jh\Import\Type\Webapi;
use Magento\Framework\ObjectManagerInterface;
use RuntimeException;
use function get_class;
/**
* @author Aydin Hassan <[email protected]>
*/
class Manager
{
/**
* @var Data
*/
private Data $config;
/**
* @var ObjectManagerInterface
*/
private ObjectManagerInterface $objectManager;
/**
* Pull this from config
*
* @var array
*/
private array $types = [
'files' => Files::class,
'db' => Db::class,
'webapi' => Webapi::class
];
public function __construct(Data $config, ObjectManagerInterface $objectManager)
{
$this->config = $config;
$this->objectManager = $objectManager;
}
public function executeImportByName(string $importName)
{
if (!$this->config->hasImport($importName)) {
throw new InvalidArgumentException(
sprintf('Cannot find configuration for import with name: "%s"', $importName)
);
}
$type = $this->config->getImportType($importName);
if (!isset($this->types[$type])) {
throw new InvalidArgumentException(
sprintf(
'Import configuration specified invalid type: "%s". Valid types are: "%s"',
$type,
implode(', ', array_keys($this->types))
)
);
}
$typeInstance = $this->objectManager->get($this->types[$type]);
if (!$typeInstance instanceof Type) {
throw new RuntimeException(
sprintf(
'Import type: "%s" does not implement require interface: "%s"',
get_class($typeInstance),
Type::class
)
);
}
return $typeInstance->run($this->config->getImportConfigByName($importName));
}
}