Passed
Push — master ( 793471...608ba9 )
by Paweł
01:59
created

Memory   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 175
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 9 2
A addExtensions() 0 8 3
A fetch() 0 17 3
A getGroupElement() 0 8 2
A getGroupCount() 0 4 1
A incrementGroupElement() 0 6 2
A fetchAll() 0 4 1
A isLast() 0 4 1
A fetchGroup() 0 4 1
A getGroupsCount() 0 4 1
A getGroups() 0 4 1
A getCount() 0 10 2
A getExtensions() 0 4 1
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Wszetko\Sitemap\Drivers\DataCollectors;
5
6
use Wszetko\Sitemap\Interfaces\DataCollector;
7
use Wszetko\Sitemap\Items\Url;
8
9
/**
10
 * Class Memory
11
 *
12
 * @package Wszetko\Sitemap\Drivers\DataCollectors
13
 */
14
class Memory implements DataCollector
15
{
16
    /**
17
     * @var array
18
     */
19
    private $items = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $element = [];
25
26
    /**
27
     * @var array
28
     */
29
    private $extensions = [];
30
31
    /**
32
     * @param Url    $item
33
     * @param string $group
34
     */
35 3
    public function add(Url $item, string $group): void
36
    {
37 3
        if (!isset($this->items[$group])) {
38 3
            $this->items[$group] = [];
39
        }
40
41 3
        $this->addExtensions($item->getExtensions());
42 3
        $this->items[$group][] = $item->toArray();
43 3
    }
44
45
    /**
46
     * @param array $extensions
47
     */
48 3
    public function addExtensions(array $extensions): void
49
    {
50 3
        foreach ($extensions as $extension) {
51 1
            foreach ($extension as $ext) {
52 1
                $this->extensions[$ext::NAMESPACE_NAME] = $ext::NAMESPACE_URL;
53
            }
54
        }
55 3
    }
56
57
    /**
58
     * @param string $group
59
     *
60
     * @return array
61
     */
62 2
    public function fetch(string $group): ?array
63
    {
64 2
        if (!isset($this->items[$group])) {
65 1
            return null;
66
        }
67
68 2
        $element = $this->getGroupElement($group);
69
70 2
        if (($element + 1) > $this->getGroupCount($group)) {
71 1
            return null;
72
        }
73
74 2
        $result = $this->items[$group][$element];
75 2
        $this->incrementGroupElement($group);
76
77 2
        return $result;
78
    }
79
80
    /**
81
     * @param string $group
82
     *
83
     * @return int|null
84
     */
85 2
    private function getGroupElement(string $group): ?int
86
    {
87 2
        if (!isset($this->element[$group])) {
88 2
            $this->element[$group] = 0;
89
        }
90
91 2
        return $this->element[$group];
92
    }
93
94
    /**
95
     * @param string $group
96
     *
97
     * @return int
98
     */
99 2
    public function getGroupCount(string $group): int
100
    {
101 2
        return count($this->items[$group]);
102
    }
103
104
    /**
105
     * @param string $group
106
     */
107 2
    private function incrementGroupElement(string $group): void
108
    {
109 2
        if (isset($this->element[$group])) {
110 2
            $this->element[$group]++;
111
        }
112 2
    }
113
114
    /**
115
     * @return array
116
     */
117 1
    public function fetchAll(): array
118
    {
119 1
        return $this->items;
120
    }
121
122
    /**
123
     * @param string $group
124
     *
125
     * @return bool
126
     */
127 1
    public function isLast(string $group): bool
128
    {
129 1
        return (bool) !isset($this->items[$group][$this->getGroupElement($group) + 1]);
130
    }
131
132
    /**
133
     * @param string $group
134
     *
135
     * @return array
136
     */
137 1
    public function fetchGroup(string $group): array
138
    {
139 1
        return $this->items[$group];
140
    }
141
142
    /**
143
     * @return int
144
     */
145 1
    public function getGroupsCount(): int
146
    {
147 1
        return count($this->getGroups());
148
    }
149
150
    /**
151
     * @return array
152
     */
153 1
    public function getGroups(): array
154
    {
155 1
        return array_keys($this->items);
156
    }
157
158
    /**
159
     * @return int
160
     */
161 1
    public function getCount(): int
162
    {
163 1
        $result = 0;
164
165 1
        foreach ($this->items as $item) {
166 1
            $result += count($item);
167
        }
168
169 1
        return $result;
170
    }
171
172
    /**
173
     * @return array
174
     */
175 1
    public function getExtensions(): array
176
    {
177 1
        return $this->extensions;
178
    }
179
180
    /**
181
     * DataCollector constructor.
182
     *
183
     * @param array|null $config
184
     */
185 5
    public function __construct(?array $config)
186
    {
187 5
    }
188
}
189