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