1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright (c) Florian Krämer (https://florian-kraemer.net) |
||
5 | * Licensed under The MIT License |
||
6 | * For full copyright and license information, please see the LICENSE.txt |
||
7 | * Redistributions of files must retain the above copyright notice. |
||
8 | * |
||
9 | * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
||
10 | * @author Florian Krämer |
||
11 | * @link https://github.com/Phauthentic |
||
12 | * @license https://opensource.org/licenses/MIT MIT License |
||
13 | */ |
||
14 | |||
15 | declare(strict_types=1); |
||
16 | |||
17 | namespace Phauthentic\Infrastructure\Storage; |
||
18 | |||
19 | use League\Flysystem\AdapterInterface; |
||
20 | use ArrayIterator; |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
21 | use RuntimeException; |
||
22 | |||
23 | /** |
||
24 | * Adapter Collection |
||
25 | */ |
||
26 | class AdapterCollection implements AdapterCollectionInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected array $adapters = []; |
||
32 | |||
33 | /** |
||
34 | * Constructor |
||
35 | */ |
||
36 | 3 | public function __construct() |
|
37 | { |
||
38 | 3 | $this->adapters = []; |
|
39 | 3 | } |
|
40 | |||
41 | /** |
||
42 | * @param string $name Name |
||
43 | * @param \League\Flysystem\AdapterInterface $adapter Adapter |
||
44 | * @return void |
||
45 | */ |
||
46 | 3 | public function add($name, AdapterInterface $adapter) |
|
47 | { |
||
48 | 3 | if ($this->has($name)) { |
|
49 | throw new RuntimeException(sprintf( |
||
50 | 'An adapter with the name `%s` already exists in the collection', |
||
51 | $name |
||
52 | )); |
||
53 | } |
||
54 | |||
55 | 3 | $this->adapters[$name] = $adapter; |
|
56 | 3 | } |
|
57 | |||
58 | /** |
||
59 | * @param string $name Name |
||
60 | * @return void |
||
61 | */ |
||
62 | public function remove(string $name): void |
||
63 | { |
||
64 | unset($this->adapters[$name]); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string $name Name |
||
69 | * @return bool |
||
70 | */ |
||
71 | 3 | public function has(string $name): bool |
|
72 | { |
||
73 | 3 | return isset($this->adapters[$name]); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $name Name |
||
78 | * @return \League\Flysystem\AdapterInterface |
||
79 | */ |
||
80 | 2 | public function get(string $name): AdapterInterface |
|
81 | { |
||
82 | 2 | if (!$this->has($name)) { |
|
83 | throw new RuntimeException(sprintf( |
||
84 | 'A factory registered with the name `%s` is not part of the collection.', |
||
85 | $name |
||
86 | )); |
||
87 | } |
||
88 | |||
89 | 2 | return $this->adapters[$name]; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Empties the collection |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | 1 | public function empty(): void |
|
98 | { |
||
99 | 1 | unset($this->adapters); |
|
100 | 1 | } |
|
101 | |||
102 | /** |
||
103 | * @return array |
||
104 | */ |
||
105 | 1 | public function getNameToClassmap(): array |
|
106 | { |
||
107 | 1 | if (empty($this->adapters)) { |
|
108 | 1 | return []; |
|
109 | } |
||
110 | |||
111 | $map = []; |
||
112 | foreach ($this->adapters as $name => $object) { |
||
113 | $map[$name] = get_class($object); |
||
114 | } |
||
115 | |||
116 | return $map; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @inheritDoc |
||
121 | */ |
||
122 | 1 | public function getIterator() |
|
123 | { |
||
124 | 1 | return new ArrayIterator($this->adapters); |
|
125 | } |
||
126 | } |
||
127 |