Failed Conditions
Pull Request — master (#4)
by Florian
02:57 queued 12s
created

AdapterCollection::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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;
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
     */
45 3
    public function add($name, AdapterInterface $adapter)
46
    {
47 3
        if ($this->has($name)) {
48
            throw new RuntimeException(sprintf(
49
                'An adapter with the name `%s` already exists in the collection',
50
                $name
51
            ));
52
        }
53
54 3
        $this->adapters[$name] = $adapter;
55 3
    }
56
57
    /**
58
     * @param string $name Name
59
     * @return void
60
     */
61
    public function remove(string $name): void
62
    {
63
        unset($this->adapters[$name]);
64
    }
65
66
    /**
67
     * @param string $name Name
68
     * @return bool
69
     */
70 3
    public function has(string $name): bool
71
    {
72 3
        return isset($this->adapters[$name]);
73
    }
74
75
    /**
76
     * @param string $name Name
77
     * @return \League\Flysystem\AdapterInterface
78
     */
79 2
    public function get(string $name): AdapterInterface
80
    {
81 2
        if (!$this->has($name)) {
82
            throw new RuntimeException(sprintf(
83
                'A factory registered with the name `%s` is not part of the collection.',
84
                $name
85
            ));
86
        }
87
88 2
        return $this->adapters[$name];
89
    }
90
91
    /**
92
     * Empties the collection
93
     *
94
     * @return void
95
     */
96 1
    public function empty(): void
97
    {
98 1
        unset($this->adapters);
99 1
    }
100
101
    /**
102
     * @return array
103
     */
104 1
    public function getNameToClassmap(): array
105
    {
106 1
        if (empty($this->adapters)) {
107 1
            return [];
108
        }
109
110
        $map = [];
111
        foreach ($this->adapters as $name => $object) {
112
            $map[$name] = get_class($object);
113
        }
114
115
        return $map;
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121 1
    public function getIterator()
122
    {
123 1
        return new ArrayIterator($this->adapters);
124
    }
125
}
126