Completed
Branch master (755019)
by Oliver
12:48 queued 05:47
created

Composite   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 68.42%

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 15
c 4
b 2
f 1
lcom 0
cbo 0
dl 0
loc 92
ccs 13
cts 19
cp 0.6842
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A attach() 0 4 1
A detach() 0 4 1
A offsetExists() 0 14 4
A offsetGet() 0 18 4
A offsetSet() 0 7 2
A offsetUnset() 0 7 2
1
<?php
2
3
namespace Metabor\KeyValue;
4
5
/**
6
 * @author otischlinger
7
 */
8
class Composite implements \ArrayAccess
9
{
10
    /**
11
     * @var \SplObjectStorage
12
     */
13
    private $container;
14
15
    /**
16
     *
17
     */
18 4
    public function __construct()
19
    {
20
        $this->container = new \SplObjectStorage();
21 4
    }
22
23
    /**
24
     * @param \ArrayAccess $keyValue
25
     */
26
    public function attach(\ArrayAccess $keyValue)
27
    {
28
        $this->container->attach($keyValue);
29
    }
30
31
    /**
32
     * @param \ArrayAccess $keyValue
33
     */
34
    public function detach(\ArrayAccess $keyValue)
35
    {
36
        $this->container->detach($keyValue);
37
    }
38
39
    /**
40
     * @see ArrayAccess::offsetExists()
41
     */
42 1
    public function offsetExists($offset)
43
    {
44
        if ($this->container->count()) {
45 1
            $result = true;
46
            /* @var $keyValue \ArrayAccess */
47 1
            foreach ($this->container as $keyValue) {
48
                $result = $result && $keyValue->offsetExists($offset);
49
            }
50
51 1
            return $result;
52
        } else {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * @see ArrayAccess::offsetGet()
59
     */
60 2
    public function offsetGet($offset)
61
    {
62 2
        $values = array();
63
        /* @var $keyValue \ArrayAccess */
64 2
        foreach ($this->container as $keyValue) {
65
            $values[] = $keyValue->offsetGet($offset);
66
        }
67
        $values = array_unique($values, SORT_REGULAR);
68
69 2
        switch (count($values)) {
70
            case 0:
71
                return;
72
            case 1:
73
                return reset($values);
74
            default:
75
                throw new \RuntimeException('Offset "' . $offset . '" is not unique!');
76
        }
77
    }
78
79
    /**
80
     * @see ArrayAccess::offsetSet()
81
     */
82 2
    public function offsetSet($offset, $value)
83
    {
84
        /* @var $keyValue \ArrayAccess */
85 2
        foreach ($this->container as $keyValue) {
86
            $keyValue->offsetSet($offset, $value);
87
        }
88 2
    }
89
    /**
90
     * @see ArrayAccess::offsetUnset()
91
     */
92
    public function offsetUnset($offset)
93
    {
94
        /* @var $keyValue \ArrayAccess */
95
        foreach ($this->container as $keyValue) {
96
            $keyValue->offsetUnset($offset);
97
        }
98
    }
99
}
100