Completed
Push — master ( 69e2c3...3de1cd )
by JHONATAN
02:37
created

TransferCollection::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Vox\Webservice;
4
5
use Closure;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Psr\Http\Message\ResponseInterface;
9
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
10
use Vox\Webservice\Proxy\ProxyFactoryInterface;
11
12
/**
13
 * Transfer collection is used as a way to keep the objects inside the unity of work and proxyed
14
 * 
15
 * @author Jhonatan Teixeira <[email protected]>
16
 */
17
class TransferCollection implements Collection
18
{
19
    private $transferName;
20
    
21
    /**
22
     * @var DenormalizerInterface
23
     */
24
    private $denormalizer;
25
    
26
    /**
27
     * @var ObjectStorageInterface
28
     */
29
    private $objectStorage;
30
    
31
    /**
32
     * @var ProxyFactoryInterface
33
     */
34
    private $proxyFactory;
35
    
36
    /**
37
     * @var TransferManagerInterface
38
     */
39
    private $transferManager;
40
41
    private $items = [];
42
43
    private $iterator;
44
    
45 7
    public function __construct(string $transferName, DenormalizerInterface $denormalizer, ResponseInterface $response)
46
    {
47 7
        $this->items = json_decode($response->getBody()->getContents() ?: '[]', true);
48
        
49 1
        $this->iterator = function () {
50 1
            foreach ($this->items as $key => $item) {
51 1
                yield $key => $this->createTransfer($item);
52
            }
53 1
        };
54
55 7
        $this->transferName = $transferName;
56 7
        $this->denormalizer = $denormalizer;
57 7
    }
58
59 6
    private function createTransfer($data)
60
    {
61 6
        if (!is_object($data)) {
62 6
            $data = $this->denormalizer->denormalize($data, $this->transferName);
63
64 6
            if ($this->proxyFactory && $this->transferManager) {
65 5
                $data = $this->proxyFactory->createProxy($data, $this->transferManager);
66
            }
67
        }
68
69 6
        if (isset($this->objectStorage) && !$this->objectStorage->contains($data)) {
70 4
            $this->objectStorage->attach($data);
71
        }
72
73 6
        return $data;
74
    }
75
76 4
    public function setObjectStorage(ObjectStorageInterface $objectStorage)
77
    {
78 4
        $this->objectStorage = $objectStorage;
79
        
80 4
        return $this;
81
    }
82
    
83 5
    public function setProxyFactory(ProxyFactoryInterface $proxyFactory)
84
    {
85 5
        $this->proxyFactory = $proxyFactory;
86
        
87 5
        return $this;
88
    }
89
90 5
    public function setTransferManager(TransferManagerInterface $transferManager)
91
    {
92 5
        $this->transferManager = $transferManager;
93
        
94 5
        return $this;
95
    }
96
97 1
    public function add($element)
98
    {
99 1
        $this->items[] = $element;
100
101 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Vox\Webservice\TransferCollection which is incompatible with the return type mandated by Doctrine\Common\Collections\Collection::add() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
102
    }
103
104
    public function clear()
105
    {
106
        $this->items = [];
107
    }
108
109 1
    public function contains($element): bool
110
    {
111 1
        return in_array($element, $this->toArray(), true);
112
    }
113
114 1
    public function isEmpty(): bool
115
    {
116 1
        return empty($this->items);
117
    }
118
119 1
    public function remove($key)
120
    {
121 1
        unset($this->items[$key]);
122 1
    }
123
124 1
    public function removeElement($element)
125
    {
126 1
        $key = array_search($element, $this->toArray(), true);
127
128 1
        $this->remove($key);
129
        
130 1
        if ($this->transferManager) {
131 1
            $this->transferManager->remove($element);
132
        }
133 1
    }
134
135
    public function containsKey($key)
136
    {
137
        return array_key_exists($key, $this->items);
138
    }
139
140 6
    public function get($key)
141
    {
142 6
        return $this->createTransfer($this->items[$key]);
143
    }
144
145 4
    public function getKeys()
146
    {
147 4
        return array_keys($this->items);
148
    }
149
150
    public function getValues()
151
    {
152
        return array_values($this->toArray());
153
    }
154
155 1
    public function set($key, $value)
156
    {
157 1
        $this->items[$key] = $value;
158 1
    }
159
160 1
    public function toArray()
161
    {
162 1
        return iterator_to_array($this->getIterator());
163
    }
164
165 4
    public function first()
166
    {
167 4
        $keys = $this->getKeys();
168
169 4
        return $this->get(reset($keys));
170
    }
171
172 1
    public function last()
173
    {
174 1
        $keys = $this->getKeys();
175
176 1
        return $this->get(end($keys));
177
    }
178
179 1
    public function key()
180
    {
181 1
        return key($this->items);
182
    }
183
184 1
    public function current()
185
    {
186 1
        return $this->createTransfer(current($this->items));
187
    }
188
189
    public function next()
190
    {
191
        return $this->createTransfer(next($this->items));
192
    }
193
194 1
    public function exists(Closure $p): bool
195
    {
196 1
        foreach ($this->getIterator() as $key => $item) {
197 1
            if ($p($key, $item)) {
198 1
                return true;
199
            }
200
        }
201
202
        return false;
203
    }
204
205 1
    public function filter(Closure $p)
206
    {
207 1
        foreach ($this->getIterator() as $item) {
208 1
            if ($p($item)) {
209 1
                yield $item;
210
            }
211
        }
212 1
    }
213
214 1
    public function forAll(Closure $p)
215
    {
216 1
        foreach ($this->getIterator() as $key => $item) {
217 1
            if (!$p($key, $item)) {
218 1
                return false;
219
            }
220
        }
221
222
        return true;
223
    }
224
225
    public function map(Closure $func)
226
    {
227
        foreach ($this->getIterator() as $item) {
228
            yield $func($item);
229
        }
230
    }
231
232 1
    public function partition(Closure $p)
233
    {
234 1
        $matches = $noMatches = array();
235
236 1
        foreach ($this->getIterator() as $key => $element) {
237 1
            if ($p($key, $element)) {
238 1
                $matches[$key] = $element;
239
            } else {
240 1
                $noMatches[$key] = $element;
241
            }
242
        }
243
244 1
        return [new ArrayCollection($matches), new ArrayCollection($noMatches)];
245
    }
246
247
    public function indexOf($element)
248
    {
249
        return array_search($element, $this->toArray(), true);
250
    }
251
252 1
    public function slice($offset, $length = null)
253
    {
254 1
        return array_slice(iterator_to_array($this->getIterator()), $offset, $length, true);
255
    }
256
257 1
    public function getIterator()
258
    {
259 1
        return call_user_func($this->iterator);
260
    }
261
262 1
    public function offsetExists($offset)
263
    {
264 1
        return isset($this->items[$offset]);
265
    }
266
267 3
    public function offsetGet($offset)
268
    {
269 3
        return $this->get($offset);
270
    }
271
272 1
    public function offsetSet($offset, $value)
273
    {
274 1
        $this->set($offset, $value);
275 1
    }
276
277 1
    public function offsetUnset($offset)
278
    {
279 1
        unset($this->items[$offset]);
280 1
    }
281
282 3
    public function count(): int
283
    {
284 3
        return count($this->items);
285
    }
286
}
287