|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Amasty\ImportExportCore\Config\ConfigClass; |
| 6 | + |
| 7 | +use Amasty\ImportExportCore\Api\Config\ConfigClass\ArgumentInterface; |
| 8 | +use Amasty\ImportExportCore\Api\Config\ConfigClass\ConfigClassInterface; |
| 9 | +use Magento\Framework\Data\Argument\InterpreterInterface; |
| 10 | +use Magento\Framework\ObjectManagerInterface; |
| 11 | + |
| 12 | +class Factory |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var ObjectManagerInterface |
| 16 | + */ |
| 17 | + private $objectManager; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var InterpreterInterface |
| 21 | + */ |
| 22 | + private $argumentInterpreter; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + ObjectManagerInterface $objectManager, |
| 26 | + InterpreterInterface $argumentInterpreter |
| 27 | + ) { |
| 28 | + $this->objectManager = $objectManager; |
| 29 | + $this->argumentInterpreter = $argumentInterpreter; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates instance based on specified config class |
| 34 | + * |
| 35 | + * @param ConfigClassInterface $configClass |
| 36 | + * @return mixed |
| 37 | + */ |
| 38 | + public function createObject(ConfigClassInterface $configClass) |
| 39 | + { |
| 40 | + return $this->objectManager->create( |
| 41 | + $configClass->getName(), |
| 42 | + [ |
| 43 | + 'config' => $this->prepareArguments( |
| 44 | + $this->convertArguments($configClass->getArguments()) |
| 45 | + ) |
| 46 | + ] |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + private function prepareArguments(array $arguments): array |
| 51 | + { |
| 52 | + $result = []; |
| 53 | + foreach ($arguments as $key => $argument) { |
| 54 | + $result[$key] = $this->argumentInterpreter->evaluate($argument); |
| 55 | + } |
| 56 | + |
| 57 | + return $result; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param ArgumentInterface[] $arguments |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + private function convertArguments(array $arguments): array |
| 65 | + { |
| 66 | + $result = []; |
| 67 | + foreach ($arguments as $argument) { |
| 68 | + $argName = $argument->getName(); |
| 69 | + $argType = $argument->getType(); |
| 70 | + |
| 71 | + $row = [ |
| 72 | + 'name' => $argName, |
| 73 | + 'xsi:type' => $argType |
| 74 | + ]; |
| 75 | + if ($argType === 'array') { |
| 76 | + $row['item'] = $this->convertArguments($argument->getItems()); |
| 77 | + } else { |
| 78 | + $row['value'] = $argument->getValue(); |
| 79 | + } |
| 80 | + |
| 81 | + $result[$argName] = $row; |
| 82 | + } |
| 83 | + |
| 84 | + return $result; |
| 85 | + } |
| 86 | +} |
0 commit comments