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

Dictionary   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 55
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 7 1
A getData() 0 4 1
A setData() 0 4 1
A get() 0 11 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