NeonResource   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 32
ccs 12
cts 13
cp 0.9231
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 3
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Translatte\Resource;
6
7
use Efabrica\Translatte\Dictionary;
8
use Efabrica\Translatte\Helper\Arr;
9
use Nette\Neon\Neon;
10
11
class NeonResource implements IResource
12
{
13
    /** @var string */
14
    private $filepath;
15
16
    /** @var string */
17
    private $lang;
18
19
    /** @var string */
20
    private $prefix;
21
22 15
    public function __construct(string $filepath, string $lang, string $prefix = '')
23
    {
24 15
        $this->filepath = $filepath;
25 15
        $this->lang = $lang;
26 15
        $this->prefix = $prefix;
27 15
    }
28
29 15
    public function load(string $lang): array
30
    {
31 15
        if ($lang !== $this->lang) {
32 15
            return [];
33
        }
34
35 15
        $content = @file_get_contents($this->filepath);
36 15
        if ($content === false) {
37
            // @TODO: exception?
38
            return [];
39
        }
40
41 15
        $records = Neon::decode($content);
42 15
        return [new Dictionary($lang, Arr::flatten($records, $this->prefix))];
43
    }
44
}
45