Test Failed
Pull Request — master (#996)
by Maxim
10:25
created

CatalogueLoader   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 0
Metric Value
wmc 17
eloc 51
dl 0
loc 108
ccs 45
cts 47
cp 0.9574
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLocales() 0 14 3
A hasLocale() 0 11 3
A getDirectories() 0 14 4
B loadCatalogue() 0 51 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Translator\Catalogue;
6
7
use Spiral\Logger\Traits\LoggerTrait;
8
use Spiral\Translator\Catalogue;
9
use Spiral\Translator\CatalogueInterface;
10
use Spiral\Translator\Config\TranslatorConfig;
11
use Symfony\Component\Finder\Finder;
12
13
final class CatalogueLoader implements LoaderInterface
14
{
15
    use LoggerTrait;
16
17 48
    public function __construct(
18
        private readonly TranslatorConfig $config
19
    ) {
20 48
    }
21
22 42
    public function hasLocale(string $locale): bool
23
    {
24 42
        $locale = \preg_replace('/[^a-zA-Z_]/', '', \mb_strtolower($locale));
25
26 42
        foreach ($this->getDirectories() as $directory) {
27
            if (\is_dir($this->config->getLocaleDirectory($locale, $directory))) {
28
                return true;
29 28
            }
30
        }
31 28
32
        return false;
33
    }
34
35 28
    public function getLocales(): array
36 28
    {
37
        $directories = $this->getDirectories();
38 28
        if ($directories === []) {
39
            return [];
40 28
        }
41 28
42
        $finder = new Finder();
43
        $locales = [];
44 28
        foreach ($finder->in($directories)->directories() as $directory) {
45
            $locales[] = $directory->getFilename();
46
        }
47 41
48
        return \array_unique($locales);
49 41
    }
50 41
51
    public function loadCatalogue(string $locale): CatalogueInterface
52 41
    {
53
        $locale = \preg_replace('/[^a-zA-Z_]/', '', \mb_strtolower($locale));
54
        $catalogue = new Catalogue($locale);
55
56 41
        if (!$this->hasLocale($locale)) {
57 41
            return $catalogue;
58
        }
59 41
60 38
        $directories = [];
61 38
        foreach ($this->getDirectories() as $directory) {
62 38
            if (\is_dir($this->config->getLocaleDirectory($locale, $directory))) {
63 38
                $directories[] = $this->config->getLocaleDirectory($locale, $directory);
64 38
            }
65 38
        }
66 38
67
        $finder = new Finder();
68
        foreach ($finder->in($directories)->files() as $file) {
69 38
            $this->getLogger()->info(
70
                \sprintf(
71 38
                    "found locale domain file '%s'",
72 1
                    $file->getFilename()
73 1
                ),
74 1
                ['file' => $file->getFilename()]
75 1
            );
76 1
77 1
            //Per application agreement domain name must present in filename
78 1
            $domain = \strstr($file->getFilename(), '.', true);
79
80 1
            if (!$this->config->hasLoader($file->getExtension())) {
81
                $this->getLogger()->warning(
82
                    \sprintf(
83 38
                        "unable to load domain file '%s', no loader found",
84 38
                        $file->getFilename()
85 38
                    ),
86 38
                    ['file' => $file->getFilename()]
87 38
                );
88 38
89 38
                continue;
90
            }
91
92 41
            $catalogue->mergeFrom(
93
                $this->config->getLoader($file->getExtension())->load(
94
                    (string)$file,
95
                    $locale,
96
                    $domain
97
                )
98
            );
99
        }
100
101
        return $catalogue;
102
    }
103
104
    /**
105
     * @return array<array-key, non-empty-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, non-empty-string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, non-empty-string>.
Loading history...
106
     */
107
    private function getDirectories(): array
108
    {
109
        $directories = [];
110
        if (\is_dir($this->config->getLocalesDirectory())) {
111
            $directories[] = $this->config->getLocalesDirectory();
112
        }
113
114
        foreach ($this->config->getDirectories() as $directory) {
115
            if (\is_dir($directory)) {
116
                $directories[] = $directory;
117
            }
118
        }
119
120
        return $directories;
121
    }
122
}
123