Passed
Push — master ( b808b9...919917 )
by Ehsan
04:54
created

Dictionary::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\utility\FileUtility;
6
7
/**
8
 * Class Dictionary.
9
 */
10
class Dictionary
11
{
12
    const DICTIONARY_DIR = 'dictionary';
13
    const DICTIONARY_FILE_SUFFIX = 'json';
14
15
    private $data;
16
17
    /**
18
     * @param $key
19
     *
20
     * @throws \Exception
21
     *
22
     * @return array|mixed
23
     */
24 11
    private function load($key)
25
    {
26 11
        $stopWordsPath = __DIR__.DIRECTORY_SEPARATOR.self::DICTIONARY_DIR.DIRECTORY_SEPARATOR.$key.'.'.
27 11
            self::DICTIONARY_FILE_SUFFIX;
28
29 11
        return (new FileUtility())->jsonFileToArray($stopWordsPath);
30
    }
31
32
    /**
33
     * @return array
34
     */
35 23
    public function getData()
36
    {
37 23
        return $this->data;
38
    }
39
40
    /**
41
     * @param array $data
42
     */
43 23
    public function setData(array $data)
44
    {
45 23
        $this->data = $data;
46 23
    }
47
48
    /**
49
     * @param $key
50
     *
51
     * @return mixed
52
     */
53 23
    public function get($key)
54
    {
55 23
        $data = $this->getData();
56
57 23
        if (!isset($data[$key])) {
58 11
            $data[$key] = $this->load($key);
59 11
            $this->setData($data);
60
        }
61
62 23
        return $data[$key];
63
    }
64
}
65