Completed
Push — master ( 209992...5259ee )
by Paweł
02:43
created

Memory::getGroupElementsCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Drivers\DataCollectors;
15
16
use Wszetko\Sitemap\Items\Url;
17
18
/**
19
 * Class Memory.
20
 *
21
 * @package Wszetko\Sitemap\Drivers\DataCollectors
22
 */
23
class Memory extends AbstractDataCollector
24
{
25
    /**
26
     * Collection of added URLs.
27
     *
28
     * @var array
29
     */
30
    private $items = [];
31
32
    /**
33
     * Number of elements in each group.
34
     *
35
     * @var int[]
36
     */
37
    private $element = [];
38
39
    /**
40
     * Array of used extensions.
41
     *
42
     * @var array
43
     */
44
    private $extensions = [];
45
46
    /**
47
     * @inheritDoc
48
     */
49 10
    public function add(Url $item, string $group): void
50
    {
51 10
        if (!isset($this->items[$group])) {
52 10
            $this->items[$group] = [];
53
        }
54
55 10
        $this->addExtensions($item->getExtensions());
56 10
        $this->items[$group][] = $item->toArray();
57 10
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 10
    public function addExtensions(array $extensions): void
63
    {
64 10
        foreach ($extensions as $extension) {
65 2
            foreach ($extension as $ext) {
66 2
                $this->extensions[$ext::NAMESPACE_NAME] = $ext::NAMESPACE_URL;
67
            }
68
        }
69 10
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 2
    public function fetch(string $group): ?array
75
    {
76 2
        if (!isset($this->items[$group])) {
77 2
            return null;
78
        }
79
80 2
        $element = $this->getGroupElementsCount($group);
81
82 2
        if (($element + 1) > $this->getGroupCount($group)) {
83 2
            return null;
84
        }
85
86 2
        $result = $this->items[$group][$element];
87 2
        $this->incrementGroupElement($group);
88
89 2
        return $result;
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95 2
    public function getGroupCount(string $group): int
96
    {
97 2
        return count($this->items[$group]);
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103 8
    public function fetchAll(?string $group = null): ?array
104
    {
105 8
        if (is_string($group) && $group !== '') {
106 6
            if (isset($this->items[$group])) {
107 6
                return $this->items[$group];
108
            }
109
110
            return null;
111
        }
112
113 2
        return $this->items;
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119 2
    public function isLast(string $group): bool
120
    {
121 2
        return !isset($this->items[$group][$this->getGroupElementsCount($group) + 1]);
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127 2
    public function fetchGroup(string $group): array
128
    {
129 2
        return $this->items[$group];
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135 2
    public function getGroupsCount(): int
136
    {
137 2
        return count($this->getGroups());
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143 2
    public function getGroups(): array
144
    {
145 2
        return array_keys($this->items);
146
    }
147
148
    /**
149
     * @inheritDoc
150
     */
151 2
    public function getCount(): int
152
    {
153 2
        $result = 0;
154
155 2
        foreach ($this->items as $item) {
156 2
            $result += count($item);
157
        }
158
159 2
        return $result;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165 2
    public function getExtensions(): array
166
    {
167 2
        return $this->extensions;
168
    }
169
170
    /**
171
     * Return number of elements in group.
172
     *
173
     * @param string $group
174
     *
175
     * @return int
176
     */
177 2
    private function getGroupElementsCount(string $group): int
178
    {
179 2
        if (!isset($this->element[$group])) {
180 2
            $this->element[$group] = 0;
181
        }
182
183 2
        return $this->element[$group];
184
    }
185
186
    /**
187
     * Increment number of elements in group.
188
     *
189
     * @param string $group
190
     */
191 2
    private function incrementGroupElement(string $group): void
192
    {
193 2
        if (isset($this->element[$group])) {
194 2
            ++$this->element[$group];
195
        }
196 2
    }
197
}
198