NeonDirectoryResource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 32
ccs 11
cts 12
cp 0.9167
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 20 4
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