Completed
Push — master ( 9ef25d...153ee0 )
by Paweł
03:23
created

Memory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
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
     * @var array
27
     */
28
    private $items = [];
29
30
    /**
31
     * @var array
32
     */
33
    private $element = [];
34
35
    /**
36
     * @var array
37
     */
38
    private $extensions = [];
39
40
    /**
41
     * @param Url    $item
42
     * @param string $group
43
     */
44 10
    public function add(Url $item, string $group): void
45
    {
46 10
        if (!isset($this->items[$group])) {
47 10
            $this->items[$group] = [];
48
        }
49
50 10
        $this->addExtensions($item->getExtensions());
51 10
        $this->items[$group][] = $item->toArray();
52 10
    }
53
54
    /**
55
     * @param array $extensions
56
     */
57 10
    public function addExtensions(array $extensions): void
58
    {
59 10
        foreach ($extensions as $extension) {
60 2
            foreach ($extension as $ext) {
61 2
                $this->extensions[$ext::NAMESPACE_NAME] = $ext::NAMESPACE_URL;
62
            }
63
        }
64 10
    }
65
66
    /**
67
     * @param string $group
68
     *
69
     * @return array
70
     */
71 2
    public function fetch(string $group): ?array
72
    {
73 2
        if (!isset($this->items[$group])) {
74 2
            return null;
75
        }
76
77 2
        $element = $this->getGroupElement($group);
78
79 2
        if (($element + 1) > $this->getGroupCount($group)) {
80 2
            return null;
81
        }
82
83 2
        $result = $this->items[$group][$element];
84 2
        $this->incrementGroupElement($group);
85
86 2
        return $result;
87
    }
88
89
    /**
90
     * @param string $group
91
     *
92
     * @return int
93
     */
94 2
    public function getGroupCount(string $group): int
95
    {
96 2
        return count($this->items[$group]);
97
    }
98
99
    /**
100
     * @param null|string $group
101
     *
102
     * @return null|array
103
     */
104 8
    public function fetchAll(?string $group = null): ?array
105
    {
106 8
        if ($group) {
107 6
            if (isset($this->items[$group])) {
108 6
                return $this->items[$group];
109
            }
110
111
            return null;
112
        }
113
114 2
        return $this->items;
115
    }
116
117
    /**
118
     * @param string $group
119
     *
120
     * @return bool
121
     */
122 2
    public function isLast(string $group): bool
123
    {
124 2
        return (bool) !isset($this->items[$group][$this->getGroupElement($group) + 1]);
125
    }
126
127
    /**
128
     * @param string $group
129
     *
130
     * @return array
131
     */
132 2
    public function fetchGroup(string $group): array
133
    {
134 2
        return $this->items[$group];
135
    }
136
137
    /**
138
     * @return int
139
     */
140 2
    public function getGroupsCount(): int
141
    {
142 2
        return count($this->getGroups());
143
    }
144
145
    /**
146
     * @return array
147
     */
148 2
    public function getGroups(): array
149
    {
150 2
        return array_keys($this->items);
151
    }
152
153
    /**
154
     * @return int
155
     */
156 2
    public function getCount(): int
157
    {
158 2
        $result = 0;
159
160 2
        foreach ($this->items as $item) {
161 2
            $result += count($item);
162
        }
163
164 2
        return $result;
165
    }
166
167
    /**
168
     * @return array
169
     */
170 2
    public function getExtensions(): array
171
    {
172 2
        return $this->extensions;
173
    }
174
175
    /**
176
     * @param string $group
177
     *
178
     * @return null|int
179
     */
180 2
    private function getGroupElement(string $group): ?int
181
    {
182 2
        if (!isset($this->element[$group])) {
183 2
            $this->element[$group] = 0;
184
        }
185
186 2
        return $this->element[$group];
187
    }
188
189
    /**
190
     * @param string $group
191
     */
192 2
    private function incrementGroupElement(string $group): void
193
    {
194 2
        if (isset($this->element[$group])) {
195 2
            ++$this->element[$group];
196
        }
197 2
    }
198
}
199