Completed
Push — master ( 362689...109521 )
by Andrew
02:43
created

Config::initDictionary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 0
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KRDigital\NamesDetector\Config;
6
7
use KRDigital\NamesDetector\Exception\InvalidDictionarySourceException;
8
9
class Config implements ConfigInterface
10
{
11
    private const EXTENSION_JSON = 'json';
12
13
    private const EXTENSION_PHP = 'php';
14
15
    private const ALLOWED_EXTENSIONS = [
16
        self::EXTENSION_JSON,
17
        self::EXTENSION_PHP,
18
    ];
19
20
    /**
21
     * @var string|null
22
     */
23
    protected $dictionaryPath;
24
25
    /**
26
     * @var array|null
27
     */
28
    protected $dictionaryData;
29
30
    /**
31
     * @var DictionaryInterface
32
     */
33
    protected $dictionary;
34
35 4
    public function __construct(string $dictionaryPath = null, array $dictionaryData = [])
36
    {
37 4
        if (empty($dictionaryData) && null === $dictionaryPath) {
38
            throw new InvalidDictionarySourceException('No input data is provided');
39
        }
40
41 4
        if (null !== $dictionaryPath && !\file_exists($dictionaryPath)) {
42 1
            throw new InvalidDictionarySourceException(\sprintf('Path %s is invalid', $dictionaryPath));
43
        }
44
45 3
        $this->dictionaryPath = $dictionaryPath;
46 3
        $this->dictionaryData = $dictionaryData;
47 3
    }
48
49 3
    public function getDictionary(): DictionaryInterface
50
    {
51 3
        if (null === $this->dictionary) {
52 3
            $this->dictionary = $this->initDictionary();
53
        }
54
55 2
        return $this->dictionary;
56
    }
57
58 3
    protected function initDictionary(): DictionaryInterface
59
    {
60 3
        if (!empty($this->dictionaryData)) {
61
            return Dictionary::fromArray($this->dictionaryData);
62
        }
63
64 3
        return $this->createDictionaryFromFile($this->dictionaryPath);
0 ignored issues
show
Bug introduced by
It seems like $this->dictionaryPath can also be of type null; however, parameter $dictionaryPath of KRDigital\NamesDetector\...ateDictionaryFromFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        return $this->createDictionaryFromFile(/** @scrutinizer ignore-type */ $this->dictionaryPath);
Loading history...
65
    }
66
67 3
    protected function createDictionaryFromFile(string $dictionaryPath): DictionaryInterface
68
    {
69 3
        $dictionaryExtension = \pathinfo($dictionaryPath, PATHINFO_EXTENSION);
70
71
        switch ($dictionaryExtension) {
72 3
            case self::EXTENSION_PHP:
73
                try {
74 1
                    $data = include $dictionaryPath;
75
76 1
                    return Dictionary::fromArray($data);
77
                } catch (\ParseError $error) {
78
                    throw new InvalidDictionarySourceException('Dictionary php file is invalid');
79
                }
80 2
            case self::EXTENSION_JSON:
81
                // JSON_THROW_ON_ERROR polyfill
82 1
                if (null === $data = \json_decode(\file_get_contents($dictionaryPath), true)) {
83
                    throw new InvalidDictionarySourceException('Dictionary json file is invalid');
84
                }
85
86 1
                return Dictionary::fromArray($data);
87
            default:
88 1
                throw new InvalidDictionarySourceException(\sprintf('Extension %s is not valid, allowed extensions are %s', $dictionaryExtension, \implode(', ', self::ALLOWED_EXTENSIONS)));
89
        }
90
    }
91
}
92