| Total Complexity | 8 |
| Total Lines | 69 |
| Duplicated Lines | 0 % |
| Coverage | 89.47% |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | abstract class AbstractFactory |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var string $allowedType |
||
| 12 | */ |
||
| 13 | protected $allowedType; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var array $config |
||
| 17 | */ |
||
| 18 | protected $config = []; |
||
| 19 | |||
| 20 | 12 | public function __construct(array $config) |
|
| 23 | 12 | } |
|
| 24 | |||
| 25 | /** |
||
| 26 | * loadConfig. |
||
| 27 | * |
||
| 28 | * @access protected |
||
| 29 | * @param array $config |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | 12 | protected function loadConfig(array $config): void |
|
| 33 | { |
||
| 34 | 12 | foreach ($config as $code => $class) { |
|
| 35 | 12 | if (!class_exists($class)) { |
|
| 36 | throw new FactoryException('Фабрика не сможет создать такой объект: '.$class); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | 12 | $this->config = $config; |
|
| 41 | 12 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * make. |
||
| 45 | * |
||
| 46 | * @access public |
||
| 47 | * @param string $code |
||
| 48 | * @return mixed |
||
| 49 | */ |
||
| 50 | 4 | public function make(string $code) |
|
| 51 | { |
||
| 52 | 4 | if (empty($this->config)) { |
|
| 53 | throw new ConfigurationException('Фабрика не инициализирована'); |
||
| 54 | } |
||
| 55 | |||
| 56 | 4 | if (! $this->entityExists($code)) { |
|
| 57 | 1 | throw new FactoryException('Фабрика не может сделать такую сущность'); |
|
| 58 | } |
||
| 59 | |||
| 60 | 3 | $resolved = new $this->config[$code]; |
|
| 61 | |||
| 62 | 3 | Assert::isInstanceOf($resolved, $this->allowedType); |
|
| 63 | |||
| 64 | 3 | return $resolved; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * entityExists. |
||
| 69 | * |
||
| 70 | * @access public |
||
| 71 | * @param string $code |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | 4 | public function entityExists(string $code): bool |
|
| 77 | } |
||
| 78 | } |
||
| 79 |