Total Complexity | 4 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | class Multiton |
||
18 | { |
||
19 | use TLang; |
||
20 | |||
21 | /** @var Factory */ |
||
22 | protected $factory = null; |
||
23 | /** @var array<string, IStorage> */ |
||
24 | protected $instances = []; |
||
25 | |||
26 | 3 | public function __construct(?Interfaces\IStTranslations $lang = null) |
|
27 | { |
||
28 | 3 | $this->setStLang($lang); |
|
29 | 3 | $this->factory = new Factory($this->getStLang()); |
|
30 | 3 | } |
|
31 | |||
32 | /** |
||
33 | * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>>|string|object|int|bool|null $params |
||
34 | * @param string|null $alias |
||
35 | * @throws StorageException |
||
36 | * @return IStorage |
||
37 | */ |
||
38 | 2 | public function lookup($params, ?string $alias = null): IStorage |
|
39 | { |
||
40 | 2 | $key = $alias ?? $this->paramsToKey($params); |
|
41 | 2 | if (!isset($this->instances[$key])) { |
|
42 | 2 | $this->instances[$key] = $this->factory->getStorage($params); |
|
43 | } |
||
44 | |||
45 | 2 | return $this->instances[$key]; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>>|string|object|int|bool|null $params |
||
50 | * @return string |
||
51 | */ |
||
52 | 1 | public function paramsToKey($params): string |
|
55 | } |
||
56 | } |
||
57 |