Passed
Push — master ( 31d695...c567f3 )
by Max
02:55
created

Manager::setDefaultDictionary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace EasyDictionary;
6
7
use EasyDictionary\Exception\InvalidConfigurationException;
8
use EasyDictionary\Exception\RuntimeException;
9
use Psr\SimpleCache\CacheInterface;
10
11
/**
12
 * Class Manager
13
 * @package EasyDictionary
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
14
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
15
class Manager
16
{
17
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
     * @var string
19
     */
20
    public $defaultDictionary = 'EasyDictionary\Dictionary\Simple';
21
22
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
     * @return string
24
     */
25
    public function getDefaultDictionary(): string
26
    {
27
        return $this->defaultDictionary;
28
    }
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @param string $defaultDictionary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
33
     */
34
    public function setDefaultDictionary(string $defaultDictionary)
35
    {
36
        $this->defaultDictionary = $defaultDictionary;
37
38
        return $this;
39
    }
40
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @return string
43
     */
44
    public function getDefaultDataProvider(): string
45
    {
46
        return $this->defaultDataProvider;
47
    }
48
49
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @param string $defaultDataProvider
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
51
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
52
     */
53
    public function setDefaultDataProvider(string $defaultDataProvider)
54
    {
55
        $this->defaultDataProvider = $defaultDataProvider;
56
57
        return $this;
58
    }
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @var string
62
     */
63
    public $defaultDataProvider = 'EasyDictionary\DataProvider\Simple';
64
65
    /**
66
     * [
67
     *      "caches" => [
68
     *          "cache_name_2" => callable
69
     *      ]
70
     *      ["defaultView" => function (...$data):\Generator {},]
71
     *      "dictionaries" => [
72
     *          "dictionary_name" => [
73
     *              ["class" => EasyDictionary\DictionaryInterface,]
74
     *              ["dataType" => "flat|array"]
75
     *              ["cache" => "cache_name"]
76
     *              ["cacheTTL" => 60,]
77
     *              "data" => [
78
     *                  ["class" => EasyDictionary\DataProviderInterface,]
79
     *                  [data provider arguments]
80
     *              ],
81
     *              ["view" => function (...$data):\Generator {},]
82
     *              ["searchFields" => [<string>, ...]]
83
     *          ],
84
     *
85
     *          ...
86
     *      ]
87
     * ]
88
     *
89
     * @var array
90
     */
91
    public $config = [];
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @var array
95
     */
96
    protected $dictionaries = [];
97
98
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
99
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
100
     * @return DictionaryInterface
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
101
     * @throws InvalidConfigurationException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
102
     * @throws RuntimeException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
103
     */
104
    public function get(string $name): DictionaryInterface
105
    {
106
        if (!isset($this->dictionaries[$name])) {
107
            $config = $this->getConfig()['dictionaries'][$name] ?? null;
108
109
            if (!$config) {
110
                throw new RuntimeException(sprintf('Dictionary with key "%s" not found', $name));
111
            }
112
113
            $this->add($this->create($name, $config));
114
        }
115
116
        return $this->dictionaries[$name];
117
    }
118
119
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
120
     * @return array
121
     */
122
    public function getConfig(): array
123
    {
124
        return $this->config;
125
    }
126
127
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
128
     * @param array $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
129
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
130
     */
131
    public function setConfig(array $config)
132
    {
133
        $this->config = $config;
134
135
        return $this;
136
    }
137
138
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
139
     * @param DictionaryInterface $dictionary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
140
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
141
     * @throws RuntimeException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
142
     */
143
    public function add(DictionaryInterface $dictionary)
144
    {
145
        $name = $dictionary->getName();
146
147
        if (isset($this->dictionaries[$name])) {
148
            throw new RuntimeException(sprintf('The dictionary with key "%s" already exists', $name));
149
        }
150
151
        $this->dictionaries[$name] = $dictionary;
152
153
        return $this;
154
    }
155
156
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $dictionaryConfig should have a doc-comment as per coding-style.
Loading history...
157
     * @param $name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
158
     * @param $dictionaryConfig
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
159
     * @return DictionaryInterface
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
160
     * @throws InvalidConfigurationException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
161
     */
162
    protected function create(string $name, array $dictionaryConfig): DictionaryInterface
163
    {
164
        $dictionaryClass = $dictionaryConfig['class'] ?? $this->getDefaultDictionary();
165
        if (!class_exists($dictionaryClass)) {
166
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $dictionaryClass));
167
        }
168
169
        $dataProviderClass = $dictionaryConfig['data']['class'] ?? $this->getDefaultDataProvider();
170
        if (!class_exists($dataProviderClass)) {
171
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $dataProviderClass));
172
        }
173
174
        /** @var DataProviderInterface $dataProvider */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
175
        $dataProvider = new $dataProviderClass($dictionaryConfig['data']);
176
177
        /** @var DictionaryInterface $dictionary */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
178
        $dictionary = new $dictionaryClass($name);
179
        $dictionary->setDataProvider($dataProvider);
180
        $dictionary->setDefaultView($dictionaryConfig['view'] ?? ($this->getConfig()['defaultView'] ?? null));
181
        $dictionary->setSearchFields($dictionaryConfig['searchFields'] ?? []);
182
183
        if (isset($dictionaryConfig['dataType'])) {
184
            $dictionary->setDataValueType($dictionaryConfig['dataType']);
185
        }
186
187
        if (isset($dictionaryConfig['cache'])) {
188
            $caches = $this->getConfig()['caches'] ?? [];
189
190 View Code Duplication
            if (!isset($caches[$dictionaryConfig['cache']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
                throw new InvalidConfigurationException(sprintf('Cache "%s" not found', $dictionaryConfig['cache']));
192
            }
193
194 View Code Duplication
            if (!is_callable($caches[$dictionaryConfig['cache']])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
                throw new InvalidConfigurationException(sprintf('Cache "%s" not callable', $dictionaryConfig['cache']));
196
            }
197
198
            $cache = $caches[$dictionaryConfig['cache']]();
199
200
            if (!($cache instanceof CacheInterface)) {
201
                throw new InvalidConfigurationException(
202
                    sprintf('Object with class "%s" does not support expected interface', get_class($cache))
203
                );
204
            }
205
206
            $dictionary->setCache($cache, $dictionaryConfig['cacheTTL'] ?? 60);
207
        }
208
209
        return $dictionary;
210
    }
211
}
212