Completed
Push — master ( 83aa6c...801091 )
by Mārtiņš
02:12
created

Collection::getTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Palladium\Component;
4
5
use Palladium\Component\Collection;
6
use Palladium\Contract\HasId;
7
8
/**
9
 * Class for handling sets of domain entities.
10
 * To use it, it needs to be extended with `buildEntity()` method implemented. This method
11
 * must return an instance of that domain entity.
12
 */
13
14
15
abstract class Collection implements \Iterator, \ArrayAccess, \Countable
16
{
17
18
    abstract protected function buildEntity();
19
20
    private $pool = [];
21
    private $indexed = [];
22
23
    private $map = [];
24
25
    private $position = 0;
26
27
    private $total = 0;
28
    private $offset = 0;
29
    private $limit = 10;
30
31
32
    /**
33
     * Add new domain entity, that is constructed using array as values. Each array key
34
     * will be attempted top match with entity's setter method and provided with
35
     * the respective array value. It returns the newly created entity.
36
     *
37
     * @param array $parameters
38
     *
39
     * @return HasId
40
     */
41 1
    public function addBlueprint(array $parameters)
42
    {
43 1
        $instance = $this->buildEntity();
44 1
        $this->populateEntity($instance, $parameters);
45
46 1
        $this->addEntity($instance);
47
48 1
        return $instance;
49
    }
50
51
52
    /** code that does the actualy population of data from the given array in blueprint */
53 1 View Code Duplication
    private function populateEntity($instance, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55 1
        foreach ((array) $parameters as $key => $value) {
56 1
            $method = 'set' . str_replace('_', '', $key);
57 1
            if (method_exists($instance, $method)) {
58 1
                $instance->{$method}($value);
59
            }
60
        }
61 1
    }
62
63
64
    /**
65
     * Method for adding already existing domain entity to the collection.
66
     *
67
     * @param HasId $entity
68
     */
69 12
    public function addEntity(HasId $entity, $key = null)
70
    {
71
72 12
        if (is_null($key) === false) {
73 2
            $this->replaceEntity($entity, $key);
74 2
            return;
75
        }
76
77 11
        $id = $entity->getId();
78
79 11
        $this->pool[] = $entity;
80
81 11
        $this->indexed[$id] = $entity;
82 11
        $this->map[$id] = $this->retrieveLastPoolKey();
83 11
    }
84
85
86 2
    private function replaceEntity(HasId $entity, $key)
87
    {
88 2
        if (isset($this->pool[$key])) {
89 1
            $this->removeIndexEntry($this->pool[$key]->getId());
90
        }
91
92 2
        $id = $entity->getId();
93
94 2
        $this->pool[$key] = $entity;
95 2
        $this->indexed[$id] = $entity;
96 2
        $this->map[$id] = $key;
97 2
    }
98
99
100 4
    private function removeIndexEntry($key)
101
    {
102 4
        unset($this->indexed[$key]);
103 4
        unset($this->map[$key]);
104 4
    }
105
106
107 11
    private function retrieveLastPoolKey()
108
    {
109 11
        end($this->pool);
110 11
        return key($this->pool);
111
    }
112
113
114
    /**
115
     * Method for getting an ordered list of IDs for items in the collection.
116
     *
117
     * @return array
118
     */
119 4
    public function getIds()
120
    {
121 4
        $keys = array_keys($this->indexed);
122 4
        sort($keys);
123 4
        return $keys;
124
    }
125
126
127
    /**
128
     * Replaces all of the domain entities with a content of some other collection
129
     *
130
     * @param Collection $replacement
131
     */
132 1
    public function replaceWith(Collection $replacement)
133
    {
134 1
        $this->pool = [];
135 1
        $this->map = [];
136 1
        $this->indexed = [];
137
138 1
        foreach ($replacement as $entity) {
139 1
            $this->addEntity($entity);
140
        }
141
142 1
    }
143
144
145
    /**
146
     * Removes an entity from collection.
147
     *
148
     * @param HasId $entity
149
     */
150 3
    public function removeEntity(HasId $entity)
151
    {
152 3
        $key = $entity->getId();
153
154 3
        if ($key !== null) {
155 3
            unset($this->pool[$this->map[$key]]);
156 3
            $this->removeIndexEntry($key);
157
        }
158 3
    }
159
160
161
    /**
162
     * Removes all od the content of the collection and resets it to pristine state.
163
     */
164 1
    public function purge()
165
    {
166 1
        $this->pool = [];
167 1
        $this->indexed = [];
168 1
        $this->map = [];
169 1
        $this->position = 0;
170 1
    }
171
172
173
    // imeplementing Countable
174 2
    public function count()
175
    {
176 2
        return count($this->pool);
177
    }
178
179
180
    // implementing Iterator
181 2
    public function rewind()
182
    {
183 2
        $this->position = 0;
184 2
    }
185
186
187 2
    public function current()
188
    {
189 2
        return $this->pool[$this->position];
190
    }
191
192
193
    /**
194
     * @codeCoverageIgnore
195
     */
196
    public function key()
197
    {
198
        return $this->position;
199
    }
200
201
202 2
    public function next()
203
    {
204 2
        ++$this->position;
205 2
    }
206
207
208 2
    public function valid()
209
    {
210 2
        return isset($this->pool[$this->position]);
211
    }
212
213
214
    // implementing ArrayAccess
215 9
    public function offsetSet($offset, $value)
216
    {
217 9
        $this->addEntity($value, $offset);
218 9
    }
219
220
221 1
    public function offsetExists($offset)
222
    {
223 1
        return isset($this->pool[$offset]);
224
    }
225
226
227 3
    public function offsetUnset($offset)
228
    {
229 3
        $this->removeEntity($this->pool[$offset]);
230 3
    }
231
232
233 5
    public function offsetGet($offset)
234
    {
235 5
        if (isset($this->pool[$offset])) {
236 5
            return $this->pool[$offset];
237
        }
238
239 2
        return null;
240
    }
241
242
243
    // pagination
244
245 1
    public function setOffset($offset)
246
    {
247 1
        $data = (int) $offset;
248
249 1
        if ($data < 0) {
250 1
            $data = 0;
251
        }
252
253 1
        $this->offset = $data;
254 1
    }
255
256
257
    /**
258
     * @codeCoverageIgnore
259
     */
260
    public function getOffset()
261
    {
262
        return $this->offset;
263
    }
264
265
266 1
    public function setLimit($limit)
267
    {
268 1
        $data = (int) $limit;
269
270 1
        if ($data < 0) {
271 1
            $data = 0;
272
        }
273
274 1
        $this->limit = $data;
275 1
    }
276
277
278
    /**
279
     * @codeCoverageIgnore
280
     */
281
    public function getLimit()
282
    {
283
        return $this->limit;
284
    }
285
286
287 1
    public function setTotal($total)
288
    {
289 1
        $data = (int) $total;
290
291 1
        if ($data < 0) {
292 1
            $data = 0;
293
        }
294
295 1
        $this->total = $data;
296 1
    }
297
298
299
    /**
300
     * @codeCoverageIgnore
301
     */
302
    public function getTotal()
303
    {
304
        return $this->total;
305
    }
306
}
307