Completed
Push — master ( 16daf9...8a3ba8 )
by Mike
03:45
created

AdjacencyList::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Version\Graph;
4
5
use Sugarcrm\UpgradeSpec\Version\OrderedList;
6
use Sugarcrm\UpgradeSpec\Version\Version;
7
8
final class AdjacencyList implements \ArrayAccess, \IteratorAggregate
9
{
10
    /**
11
     * @var array
12
     */
13
    private $container = [];
14
15
    /**
16
     * AdjacencyList constructor.
17
     *
18
     * @param array $edges
19
     */
20
    public function __construct(array $edges = [])
21
    {
22
        $versions = new OrderedList(call_user_func_array('array_merge', $edges));
23
24
        // init container
25
        foreach ($versions as $version) {
26
            $this->container[(string) $version] = new OrderedList();
27
        }
28
29
        // add edges
30
        foreach ($edges as $edge) {
31
            $this->container[(string) $edge[0]] = ($this->container[(string) $edge[0]])->addVersions([$edge[1]]);
32
        }
33
34
        // add parent edges
35
        foreach ($versions as $version) {
36
            $minorVersions = $versions->filter(function (Version $minorVersion) use ($version) {
37
                return $minorVersion->isChildOf($version);
38
            });
39
40
            // link all minor versions to existing parent versions
41
            foreach ($minorVersions as $minorVersion) {
42
                $this->container[(string) $minorVersion] = ($this->container[(string) $minorVersion])->addVersions([$version]);
43
            }
44
        }
45
    }
46
47
    /**
48
     * @param mixed $offset
49
     * @param mixed $value
50
     */
51
    public function offsetSet($offset, $value)
52
    {
53
        $this->container[(string) $offset] = $value;
54
    }
55
56
    /**
57
     * @param mixed $offset
58
     *
59
     * @return bool
60
     */
61
    public function offsetExists($offset)
62
    {
63
        return isset($this->container[(string) $offset]);
64
    }
65
66
    /**
67
     * @param mixed $offset
68
     */
69
    public function offsetUnset($offset)
70
    {
71
        unset($this->container[(string) $offset]);
72
    }
73
74
    /**
75
     * @param mixed $offset
76
     *
77
     * @return mixed|null
78
     */
79
    public function offsetGet($offset)
80
    {
81
        return isset($this->container[(string) $offset]) ? $this->container[(string) $offset] : null;
82
    }
83
84
    /**
85
     * Retrieve an external iterator.
86
     *
87
     * @return \Traversable
88
     */
89
    public function getIterator()
90
    {
91
        return new \ArrayIterator($this->container);
92
    }
93
}
94