| Total Complexity | 9 |
| Total Lines | 77 |
| Duplicated Lines | 0 % |
| Coverage | 80.95% |
| 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 | /** |
||
| 21 | * @var array $resolved |
||
| 22 | */ |
||
| 23 | protected $resolved = []; |
||
| 24 | |||
| 25 | 9 | public function __construct(array $config) |
|
| 28 | 9 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * make. |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * @param string $code |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | 1 | public function make(string $code) |
|
| 38 | { |
||
| 39 | 1 | if (! empty($this->resolved[$code])) { |
|
| 40 | return $this->resolved[$code]; |
||
| 41 | } |
||
| 42 | |||
| 43 | 1 | if (empty($this->config)) { |
|
| 44 | throw new ConfigurationException('Фабрика не инициализирована'); |
||
| 45 | } |
||
| 46 | |||
| 47 | 1 | if (is_callable($this->config[$code])) { |
|
| 48 | $resolved = $this->config[$code](); |
||
| 49 | 1 | } elseif (is_string($code)) { |
|
|
|
|||
| 50 | 1 | $resolved = $this->getResolvedFromString($code); |
|
| 51 | } |
||
| 52 | |||
| 53 | 1 | Assert::isInstanceOf($resolved, $this->allowedType); |
|
| 54 | |||
| 55 | 1 | $this->resolved[$code] = $resolved; |
|
| 56 | |||
| 57 | 1 | return $this->resolved[$code]; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * getResolvedFromString. |
||
| 62 | * |
||
| 63 | * @access protected |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | 1 | protected function getResolvedFromString(string $code) |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * entityExists. |
||
| 77 | * |
||
| 78 | * @access public |
||
| 79 | * @param string $code |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | 1 | public function entityExists(string $code): bool |
|
| 85 | } |
||
| 86 | } |
||
| 87 |