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