Passed
Push — trunk ( 8c1bee...f791bc )
by Christian
12:38 queued 15s
created

SnippetFileLoader   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadSnippetFilesIntoCollection() 0 5 1
A loadPluginSnippets() 0 19 6
A loadAppSnippets() 0 7 3
B loadSnippetFilesInDir() 0 55 6
A __construct() 0 6 1
A getAuthorFromBundle() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\Snippet\Files;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Exception;
7
use Shopware\Core\Framework\App\ActiveAppsLoader;
8
use Shopware\Core\Framework\Bundle;
9
use Shopware\Core\Framework\Log\Package;
10
use Shopware\Core\Framework\Plugin;
11
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\HttpKernel\KernelInterface;
13
14
#[Package('system-settings')]
15
class SnippetFileLoader implements SnippetFileLoaderInterface
16
{
17
    /**
18
     * @internal
19
     */
20
    public function __construct(
21
        private readonly KernelInterface $kernel,
22
        private readonly Connection $connection,
23
        private readonly AppSnippetFileLoader $appSnippetFileLoader,
24
        private readonly ActiveAppsLoader $activeAppsLoader
25
    ) {
26
    }
27
28
    public function loadSnippetFilesIntoCollection(SnippetFileCollection $snippetFileCollection): void
29
    {
30
        $this->loadPluginSnippets($snippetFileCollection);
31
32
        $this->loadAppSnippets($snippetFileCollection);
33
    }
34
35
    private function loadPluginSnippets(SnippetFileCollection $snippetFileCollection): void
36
    {
37
        foreach ($this->kernel->getBundles() as $bundle) {
38
            if (!$bundle instanceof Bundle) {
39
                continue;
40
            }
41
42
            $snippetDir = $bundle->getPath() . '/Resources/snippet';
43
44
            if (!is_dir($snippetDir)) {
45
                continue;
46
            }
47
48
            foreach ($this->loadSnippetFilesInDir($snippetDir, $bundle) as $snippetFile) {
49
                if ($snippetFileCollection->hasFileForPath($snippetFile->getPath())) {
50
                    continue;
51
                }
52
53
                $snippetFileCollection->add($snippetFile);
54
            }
55
        }
56
    }
57
58
    private function loadAppSnippets(SnippetFileCollection $snippetFileCollection): void
59
    {
60
        foreach ($this->activeAppsLoader->getActiveApps() as $app) {
61
            $snippetFiles = $this->appSnippetFileLoader->loadSnippetFilesFromApp($app['author'] ?? '', $app['path']);
62
            foreach ($snippetFiles as $snippetFile) {
63
                $snippetFile->setTechnicalName($app['name']);
64
                $snippetFileCollection->add($snippetFile);
65
            }
66
        }
67
    }
68
69
    /**
70
     * @return AbstractSnippetFile[]
71
     */
72
    private function loadSnippetFilesInDir(string $snippetDir, Bundle $bundle): array
73
    {
74
        $finder = new Finder();
75
        $finder->in($snippetDir)
76
            ->files()
77
            ->name('*.json');
78
79
        try {
80
            /** @var array<string, string> $authors */
81
            $authors = $this->connection->fetchAllKeyValue('
82
                SELECT `base_class` AS `baseClass`, `author`
83
                FROM `plugin`
84
            ');
85
        } catch (Exception) {
86
            // to get it working in setup without a database connection
87
            $authors = [];
88
        }
89
90
        $snippetFiles = [];
91
92
        foreach ($finder->getIterator() as $fileInfo) {
93
            $nameParts = explode('.', $fileInfo->getFilenameWithoutExtension());
94
95
            $snippetFile = null;
96
            switch (\count($nameParts)) {
97
                case 2:
98
                    $snippetFile = new GenericSnippetFile(
99
                        implode('.', $nameParts),
100
                        $fileInfo->getPathname(),
101
                        $nameParts[1],
102
                        $this->getAuthorFromBundle($bundle, $authors),
103
                        false,
104
                        $bundle->getName()
105
                    );
106
107
                    break;
108
                case 3:
109
                    $snippetFile = new GenericSnippetFile(
110
                        implode('.', [$nameParts[0], $nameParts[1]]),
111
                        $fileInfo->getPathname(),
112
                        $nameParts[1],
113
                        $this->getAuthorFromBundle($bundle, $authors),
114
                        $nameParts[2] === 'base',
115
                        $bundle->getName()
116
                    );
117
118
                    break;
119
            }
120
121
            if ($snippetFile) {
122
                $snippetFiles[] = $snippetFile;
123
            }
124
        }
125
126
        return $snippetFiles;
127
    }
128
129
    /**
130
     * @param array<string, string> $authors
131
     */
132
    private function getAuthorFromBundle(Bundle $bundle, array $authors): string
133
    {
134
        if (!$bundle instanceof Plugin) {
135
            return 'Shopware';
136
        }
137
138
        return $authors[$bundle::class] ?? '';
139
    }
140
}
141