Completed
Push — master ( 77f195...b72026 )
by JHONATAN
02:28
created

ObjectStorage::getIterator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Vox\Webservice;
4
5
use InvalidArgumentException;
6
use Metadata\MetadataFactoryInterface;
7
8
class ObjectStorage implements ObjectStorageInterface
9
{
10
    use MetadataTrait;
11
    
12
    private $storage = [];
13
    
14 13
    public function __construct(MetadataFactoryInterface $metadataFactory)
15
    {
16 13
        $this->metadataFactory = $metadataFactory;
17 13
    }
18
    
19 8
    public function contains($object): bool
20
    {
21 8
        return isset($this->storage[get_class($object)][$this->getIdValue($object)]);
22
    }
23
    
24 8
    public function attach($object)
25
    {
26 8
        $className = get_class($object);
27 8
        $id        = $this->getIdValue($object);
28
        
29 8
        if (!$id) {
30
            throw new InvalidArgumentException('object has no id value');
31
        }
32
        
33 8
        $this->storage[$className][$id] = $object;
34 8
    }
35
    
36 5
    public function detach($object)
37
    {
38 5
        unset($this->storage[get_class($object)][$this->getIdValue($object)]);
39 5
    }
40
41 6
    public function getIterator()
42
    {
43 6
        $items = [];
44
        
45 6
        foreach ($this->storage as $transferData) {
46 5
            foreach ($transferData as $item) {
47 5
                $items[] = $item;
48
            }
49
        }
50
        
51 6
        return new \ArrayIterator($items);
52
    }
53
54 4
    public function isEquals($object): bool
55
    {
56 4
        return $object == $this->storage[get_class($object)][$this->getIdValue($object)];
57
    }
58
}
59