Completed
Push — 1.0 ( de14e4...537877 )
by Bernhard
57:08 queued 17:45
created

ArrayResourceCollection   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.97%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 24
c 4
b 3
f 0
lcom 1
cbo 3
dl 0
loc 192
ccs 46
cts 59
cp 0.7797
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 4 1
A set() 0 4 1
A remove() 0 4 1
A has() 0 4 1
A clear() 0 4 1
A keys() 0 4 1
A isEmpty() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetUnset() 0 4 1
A count() 0 4 1
A getIterator() 0 4 1
A toArray() 0 4 1
A get() 0 11 2
A offsetSet() 0 8 2
A getPaths() 0 7 1
A getNames() 0 7 1
A replace() 0 6 2
A merge() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the puli/repository 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\Repository\Resource\Collection;
13
14
use InvalidArgumentException;
15
use IteratorAggregate;
16
use OutOfBoundsException;
17
use Puli\Repository\Api\Resource\PuliResource;
18
use Puli\Repository\Api\ResourceCollection;
19
use Puli\Repository\Api\UnsupportedResourceException;
20
use Puli\Repository\Resource\Iterator\ResourceCollectionIterator;
21
use Traversable;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * A collection of {@link PuliResource} instances backed by an array.
26
 *
27
 * @since  1.0
28
 *
29
 * @author Bernhard Schussek <[email protected]>
30
 */
31
class ArrayResourceCollection implements IteratorAggregate, ResourceCollection
32
{
33
    /**
34
     * @var PuliResource[]|Traversable
35
     */
36
    private $resources;
37
38
    /**
39
     * Creates a new collection.
40
     *
41
     * You can pass the resources that you want to initially store in the
42
     * collection as argument.
43
     *
44
     * @param PuliResource[]|Traversable $resources The resources to store in the collection.
45
     *
46
     * @throws InvalidArgumentException     If the resources are not an array
47
     *                                      and not a traversable object.
48
     * @throws UnsupportedResourceException If a resource does not implement
49
     *                                      {@link PuliResource}.
50
     */
51 692
    public function __construct($resources = array())
52
    {
53 692
        $this->replace($resources);
54 689
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 4
    public function add(PuliResource $resource)
60
    {
61 4
        $this->resources[] = $resource;
62 4
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 97
    public function set($key, PuliResource $resource)
68
    {
69 97
        $this->resources[$key] = $resource;
70 97
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 83
    public function get($key)
76
    {
77 83
        if (!isset($this->resources[$key])) {
78 1
            throw new OutOfBoundsException(sprintf(
79 1
                'The offset "%s" does not exist.',
80
                $key
81
            ));
82
        }
83
84 82
        return $this->resources[$key];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function remove($key)
91
    {
92 2
        unset($this->resources[$key]);
93 2
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function has($key)
99
    {
100 1
        return isset($this->resources[$key]);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function clear()
107
    {
108 1
        $this->resources = array();
109 1
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function keys()
115
    {
116
        return array_keys($this->resources);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 692
    public function replace($resources)
123
    {
124 692
        Assert::allIsInstanceOf($resources, 'Puli\Repository\Api\Resource\PuliResource');
125
126 689
        $this->resources = is_array($resources) ? $resources : iterator_to_array($resources);
127 689
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 3
    public function merge($resources)
133
    {
134 3
        Assert::allIsInstanceOf($resources, 'Puli\Repository\Api\Resource\PuliResource');
135
136
        // only start merging after validating all resources
137 1
        foreach ($resources as $resource) {
138 1
            $this->resources[] = $resource;
139
        }
140 1
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function isEmpty()
146
    {
147 1
        return 0 === count($this->resources);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function offsetExists($key)
154
    {
155
        return $this->has($key);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 73
    public function offsetGet($key)
162
    {
163 73
        return $this->get($key);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 97
    public function offsetSet($key, $value)
170
    {
171 97
        if (null !== $key) {
172 96
            $this->set($key, $value);
173
        } else {
174 1
            $this->add($value);
175
        }
176 97
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function offsetUnset($key)
182
    {
183
        $this->remove($key);
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getPaths()
190
    {
191
        return array_map(
192
            function (PuliResource $resource) { return $resource->getPath(); },
193
            $this->resources
194
        );
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function getNames()
201
    {
202
        return array_map(
203
            function (PuliResource $resource) { return $resource->getName(); },
204
            $this->resources
205
        );
206
    }
207
208 145
    public function count()
209
    {
210 145
        return count($this->resources);
211
    }
212
213 640
    public function getIterator($mode = ResourceCollectionIterator::KEY_AS_CURSOR)
214
    {
215 640
        return new ResourceCollectionIterator($this, $mode);
216
    }
217
218 669
    public function toArray()
219
    {
220 669
        return $this->resources;
221
    }
222
}
223