Passed
Push — trunk ( 8f893a...1d56b4 )
by Christian
14:03 queued 16s
created

SnippetFileCollection::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\Snippet\Files;
4
5
use Shopware\Core\Framework\Log\Package;
6
use Shopware\Core\Framework\Struct\Collection;
7
use Shopware\Core\System\Snippet\Exception\InvalidSnippetFileException;
8
9
/**
10
 * @extends Collection<AbstractSnippetFile>
11
 */
12
#[Package('system-settings')]
13
class SnippetFileCollection extends Collection
14
{
15
    /**
16
     * @var array<string, bool>|null
17
     */
18
    private ?array $mapping = null;
19
20
    /**
21
     * @param AbstractSnippetFile $snippetFile
22
     */
23
    public function add($snippetFile): void
24
    {
25
        $this->mapping = null;
26
        $this->set(null, $snippetFile);
27
    }
28
29
    public function get($key): ?AbstractSnippetFile
30
    {
31
        if ($this->has($key)) {
32
            return $this->elements[$key];
33
        }
34
35
        return $this->getByName($key);
36
    }
37
38
    public function set($key, $element): void
39
    {
40
        $this->mapping = null;
41
        parent::set($key, $element);
42
    }
43
44
    public function clear(): void
45
    {
46
        $this->mapping = null;
47
        parent::clear();
48
    }
49
50
    public function remove($key): void
51
    {
52
        $this->mapping = null;
53
        parent::remove($key);
54
    }
55
56
    public function getByName(string $key): ?AbstractSnippetFile
57
    {
58
        foreach ($this->elements as $index => $element) {
59
            if ($element->getName() === $key) {
60
                return $this->elements[$index];
61
            }
62
        }
63
64
        return null;
65
    }
66
67
    /**
68
     * @return array<int, array<string, mixed>>
69
     */
70
    public function getFilesArray(bool $isBase = true): array
71
    {
72
        return array_filter($this->toArray(), fn ($file) => $file['isBase'] === $isBase);
73
    }
74
75
    /**
76
     * @return array<int, array<string, mixed>>
77
     */
78
    public function toArray(): array
79
    {
80
        $data = [];
81
        foreach ($this->getListSortedByIso() as $isoFiles) {
82
            foreach ($isoFiles as $snippetFile) {
83
                $data[] = [
84
                    'name' => $snippetFile->getName(),
85
                    'iso' => $snippetFile->getIso(),
86
                    'path' => $snippetFile->getPath(),
87
                    'author' => $snippetFile->getAuthor(),
88
                    'isBase' => $snippetFile->isBase(),
89
                ];
90
            }
91
        }
92
93
        return $data;
94
    }
95
96
    /**
97
     * @return array<string>
98
     */
99
    public function getIsoList(): array
100
    {
101
        return array_keys($this->getListSortedByIso());
102
    }
103
104
    /**
105
     * @return array<int, AbstractSnippetFile>
106
     */
107
    public function getSnippetFilesByIso(string $iso): array
108
    {
109
        $list = $this->getListSortedByIso();
110
111
        return $list[$iso] ?? [];
112
    }
113
114
    /**
115
     * @throws InvalidSnippetFileException
116
     */
117
    public function getBaseFileByIso(string $iso): AbstractSnippetFile
118
    {
119
        foreach ($this->getSnippetFilesByIso($iso) as $file) {
120
            if (!$file->isBase()) {
121
                continue;
122
            }
123
124
            return $file;
125
        }
126
127
        throw new InvalidSnippetFileException($iso);
128
    }
129
130
    public function getApiAlias(): string
131
    {
132
        return 'snippet_file_collection';
133
    }
134
135
    public function hasFileForPath(string $filePath): bool
136
    {
137
        if ($this->mapping === null) {
138
            $this->mapping = [];
139
            foreach ($this->elements as $element) {
140
                $this->mapping[(string) realpath($element->getPath())] = true;
141
            }
142
        }
143
144
        return isset($this->mapping[realpath($filePath)]);
145
    }
146
147
    protected function getExpectedClass(): ?string
148
    {
149
        return AbstractSnippetFile::class;
150
    }
151
152
    /**
153
     * @return array<string, array<int, AbstractSnippetFile>>
154
     */
155
    private function getListSortedByIso(): array
156
    {
157
        $list = [];
158
159
        /** @var AbstractSnippetFile $element */
160
        foreach ($this->getIterator() as $element) {
161
            $list[$element->getIso()][] = $element;
162
        }
163
164
        return $list;
165
    }
166
}
167