ServerCollection   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 185
wmc 20
lcom 1
cbo 2
ccs 48
cts 48
cp 1
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A contains() 0 4 1
A clear() 0 4 1
A offsetUnset() 0 4 1
A getIterator() 0 4 1
A count() 0 4 1
A __construct() 0 4 1
A add() 0 4 1
A get() 0 8 2
A remove() 0 4 1
A getServerNames() 0 4 1
A replace() 0 5 1
A merge() 0 6 2
A isEmpty() 0 4 1
A toArray() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Api\Server;
13
14
use ArrayAccess;
15
use ArrayIterator;
16
use Countable;
17
use IteratorAggregate;
18
use LogicException;
19
20
/**
21
 * A collection of {@link Server} instances.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class ServerCollection implements IteratorAggregate, ArrayAccess, Countable
28
{
29
    /**
30
     * @var Server[]
31
     */
32
    private $servers = array();
33
34
    /**
35
     * Creates the collection.
36
     *
37
     * @param Server[] $servers The servers to initially fill into the
38
     *                          collection.
39
     */
40 147
    public function __construct(array $servers = array())
41
    {
42 147
        $this->merge($servers);
43 147
    }
44
45
    /**
46
     * Adds a server to the collection.
47
     *
48
     * @param Server $server The server to add.
49
     */
50 126
    public function add(Server $server)
51
    {
52 126
        $this->servers[$server->getName()] = $server;
53 126
    }
54
55
    /**
56
     * Returns the server with the given name.
57
     *
58
     * @param string $serverName The server name.
59
     *
60
     * @return Server The server.
61
     *
62
     * @throws NoSuchServerException If the server does not exist.
63
     */
64 22
    public function get($serverName)
65
    {
66 22
        if (!isset($this->servers[$serverName])) {
67 3
            throw NoSuchServerException::forServerName($serverName);
68
        }
69
70 19
        return $this->servers[$serverName];
71
    }
72
73
    /**
74
     * Removes a server from the collection.
75
     *
76
     * If the server does not exist, this method does nothing.
77
     *
78
     * @param string $serverName The server name.
79
     */
80 15
    public function remove($serverName)
81
    {
82 15
        unset($this->servers[$serverName]);
83 15
    }
84
85
    /**
86
     * Returns whether a server exists.
87
     *
88
     * @param string $serverName The server name.
89
     *
90
     * @return bool Whether the server exists.
91
     */
92 31
    public function contains($serverName)
93
    {
94 31
        return isset($this->servers[$serverName]);
95
    }
96
97
    /**
98
     * Removes all servers from the collection.
99
     */
100 9
    public function clear()
101
    {
102 9
        $this->servers = array();
103 9
    }
104
105
    /**
106
     * Returns the names of all servers in the collection.
107
     *
108
     * @return string[] The server names.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
109
     */
110 1
    public function getServerNames()
111
    {
112 1
        return array_keys($this->servers);
113
    }
114
115
    /**
116
     * Replaces the collection contents with the given servers.
117
     *
118
     * @param Server[] $servers The install servers to set.
119
     */
120 7
    public function replace(array $servers)
121
    {
122 7
        $this->clear();
123 7
        $this->merge($servers);
124 7
    }
125
126
    /**
127
     * Merges the given servers into the collection.
128
     *
129
     * @param Server[] $servers The install servers to add.
130
     */
131 147
    public function merge(array $servers)
132
    {
133 147
        foreach ($servers as $server) {
134 76
            $this->add($server);
135
        }
136 147
    }
137
138
    /**
139
     * Returns whether the collection is empty.
140
     *
141
     * @return bool Returns `true` if the collection contains no servers and
142
     *              `false` otherwise.
143
     */
144 5
    public function isEmpty()
145
    {
146 5
        return 0 === count($this->servers);
147
    }
148
149
    /**
150
     * Returns the collection contents as array.
151
     *
152
     * @return Server[] The servers in the collection indexed by their
153
     *                  names.
154
     */
155 36
    public function toArray()
156
    {
157 36
        return $this->servers;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 1
    public function offsetExists($serverName)
164
    {
165 1
        return $this->contains($serverName);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 1
    public function offsetGet($serverName)
172
    {
173 1
        return $this->get($serverName);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 2
    public function offsetSet($key, $server)
180
    {
181 2
        if (null !== $key) {
182 1
            throw new LogicException('Keys are not accepted when setting a value by array access.');
183
        }
184
185 1
        $this->add($server);
186 1
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1
    public function offsetUnset($serverName)
192
    {
193 1
        $this->remove($serverName);
194 1
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 47
    public function getIterator()
200
    {
201 47
        return new ArrayIterator($this->servers);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 1
    public function count()
208
    {
209 1
        return count($this->servers);
210
    }
211
}
212