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

ObjectStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 49
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A contains() 0 3 1
A detach() 0 3 1
A attach() 0 10 2
A isEquals() 0 3 1
A getIterator() 0 11 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