Completed
Push — master ( 4b16c6...b82f61 )
by Paweł
01:55
created

Memory::addExtensions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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