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

CatalogueLoader::loadCatalogue()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6.0012

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 51
ccs 29
cts 30
cp 0.9667
rs 8.8337
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 6.0012

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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