Completed
Push — master ( c1ca46...1342cf )
by Alexpts
02:44
created

Collection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 107
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addItem() 0 9 2
A removeItem() 0 12 3
A removeItemWithoutPriority() 0 10 3
A has() 0 10 3
A getItems() 0 4 1
A getSortedItems() 0 7 1
A getFlatSortedItems() 0 14 3
A flush() 0 5 1
1
<?php
2
declare(strict_types = 1);
3
namespace PTS\Tools;
4
5
class Collection implements CollectionInterface
6
{
7
    /** @var array */
8
    protected $items = [];
9
10
    /**
11
     * @param string $name
12
     * @param mixed $item
13
     * @param int $priority
14
     *
15
     * @return $this
16
     *
17
     * @throws DuplicateKeyException
18
     */
19 8
    public function addItem(string $name, $item, int $priority = 50)
20
    {
21 8
        if ($this->has($name)) {
22 1
            throw new DuplicateKeyException('Item with name '.$name.' already defined');
23
        }
24
25 8
        $this->items[$priority][$name] = $item;
26 8
        return $this;
27
    }
28
29
    /**
30
     * @param string $name
31
     * @param null|int $priority
32
     *
33
     * @return $this
34
     */
35 2
    public function removeItem(string $name, int $priority = null)
36
    {
37 2
        if ($priority !== null) {
38 1
            if (isset($this->items[$priority][$name])) {
39 1
                unset($this->items[$priority][$name]);
40
            }
41
42 1
            return $this;
43
        }
44
45 1
        return $this->removeItemWithoutPriority($name);
46
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @return $this
52
     */
53 1
    protected function removeItemWithoutPriority(string $name)
54
    {
55 1
        foreach ($this->items as $itemPriority => $items) {
56 1
            if (isset($items[$name])) {
57 1
                unset($this->items[$itemPriority][$name]);
58
            }
59
        }
60
61 1
        return $this;
62
    }
63
64 8
    public function has(string $name) : bool
65
    {
66 8
        foreach ($this->items as $items) {
67 3
            if (isset($items[$name])) {
68 3
                return true;
69
            }
70
        }
71
72 8
        return false;
73
    }
74
75 7
    public function getItems() : array
76
    {
77 7
        return $this->items;
78
    }
79
80 2
    public function getSortedItems() : array
81
    {
82 2
        $items = $this->getItems();
83 2
        krsort($items, SORT_NUMERIC);
84
85 2
        return $items;
86
    }
87
88 1
    public function getFlatSortedItems() : array
89
    {
90 1
        $sortedItems = $this->getSortedItems();
91 1
        $flatItems = [];
92
93
        /** @var array $items */
94 1
        foreach ($sortedItems as $items) {
95 1
            foreach ($items as $item) {
96 1
                $flatItems[] = $item;
97
            }
98
        }
99
100 1
        return $flatItems;
101
    }
102
103
    /**
104
     * @return $this
105
     */
106 1
    public function flush()
107
    {
108 1
        $this->items = [];
109 1
        return $this;
110
    }
111
}
112