NeonDirectoryResource::load()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 20
ccs 7
cts 8
cp 0.875
rs 9.9
cc 4
nc 3
nop 1
crap 4.0312
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Translatte\Resource;
6
7
use Nette\Utils\Finder;
8
use SplFileInfo;
9
10
class NeonDirectoryResource implements IResource
11
{
12
    private array $directories;
13
14
    private array $ignoredPrefixes;
15
16
    public function __construct(array $directories, array $ignoredPrefixes = ['messages'])
17 15
    {
18
        $this->directories = $directories;
19 15
        $this->ignoredPrefixes = $ignoredPrefixes;
20 15
    }
21 15
22
    public function load(string $lang): array
23 15
    {
24
        $directories = [];
25 15
        /** @var SplFileInfo $file */
26
        foreach (Finder::find('*.*.neon')->from(...$this->directories) as $file) {
27 15
            $matchCount = preg_match(
28 15
                '~^(?P<prefix>.*?)\.(?P<lang>[^\.]+)\.(?P<format>[^\.]+)$~',
29
                $file->getFilename(),
30
                $matches
31
            );
32 15
33 15
            if ($matchCount !== 1) {
34
                continue;
35
            }
36 15
37
            $resource = new NeonResource($file->getPathname(), $matches['lang'], in_array($matches['prefix'], $this->ignoredPrefixes, true) ? '' : $matches['prefix'] . '.');
38
            $directories[] = $resource->load($lang);
39
        }
40
41
        return array_merge(...$directories);
42
    }
43
}
44