Passed
Branch master (d8ab5e)
by Max
05:15 queued 02:56
created

Manager::getDefaultDictionary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyDictionary;
6
7
use EasyDictionary\Exception\InvalidConfigurationException;
8
use EasyDictionary\Exception\RuntimeException;
9
use EasyDictionary\Interfaces\ConfigInterface;
10
use EasyDictionary\Interfaces\DataProviderInterface;
11
use EasyDictionary\Interfaces\DictionaryInterface;
12
13
/**
14
 * Class Manager
15
 *
16
 * @package EasyDictionary
17
 */
18
class Manager
19
{
20
    /**
21
     * @var ConfigInterface
22
     */
23
    protected $config = null;
24
25
    /**
26
     * @var array
27
     */
28
    protected $dictionaries = [];
29
30
    /**
31
     * @param ConfigInterface $config
32
     */
33 11
    public function __construct(ConfigInterface $config = null)
34
    {
35 11
        if (!is_null($config)) {
36 2
            $this->setConfig($config);
37
        }
38 11
    }
39
40
    /**
41
     * Returns Dictionary object
42
     *
43
     * @param string $name
44
     *
45
     * @return DictionaryInterface
46
     * @throws InvalidConfigurationException
47
     * @throws RuntimeException
48
     */
49 10
    public function get(string $name): ?DictionaryInterface
50
    {
51 10
        if (!isset($this->dictionaries[$name])) {
52 9
            $config = $this->getConfig();
53 9
            if (is_null($config)) {
54 1
                throw new RuntimeException(sprintf('Config not found for dictionary %s', $name));
55
            }
56
57 8
            $dictionaryConfig = $config->getDictionaryConfig()[$name] ?? null;
58 8
            if (!$dictionaryConfig) {
59 1
                throw new RuntimeException(sprintf('Dictionary with key "%s" not found', $name));
60
            }
61
62 7
            $this->add($this->create($name, $dictionaryConfig, $config));
63
        }
64
65 3
        return $this->dictionaries[$name] ?? null;
66
    }
67
68
    /**
69
     * @return ConfigInterface|null
70
     */
71 9
    public function getConfig(): ?ConfigInterface
72
    {
73 9
        return $this->config;
74
    }
75
76
    /**
77
     * @param ConfigInterface $config
78
     *
79
     * @return $this
80
     */
81 8
    public function setConfig(ConfigInterface $config)
82
    {
83 8
        $this->config = $config;
84
85 8
        return $this;
86
    }
87
88
    /**
89
     * @param DictionaryInterface $dictionary
90
     *
91
     * @return $this
92
     * @throws RuntimeException
93
     */
94 4
    public function add(DictionaryInterface $dictionary)
95
    {
96 4
        $name = $dictionary->getName();
97
98 4
        if (isset($this->dictionaries[$name])) {
99 1
            throw new RuntimeException(sprintf('The dictionary with key "%s" already exists', $name));
100
        }
101
102 4
        $this->dictionaries[$name] = $dictionary;
103
104 4
        return $this;
105
    }
106
107
    /**
108
     * @param string $name
109
     * @param array $dictionaryConfig
110
     * @param ConfigInterface $config
111
     * @return DictionaryInterface
112
     * @throws InvalidConfigurationException
113
     */
114 7
    protected function create(string $name, array $dictionaryConfig, ConfigInterface $config): DictionaryInterface
115
    {
116 7
        $dataProvider = $this->createDataProvider(
117 7
            $dictionaryConfig['data']['class'] ?? $config->getDefaultDataProviderClass(),
118 7
            $dictionaryConfig['data'] ?? []
119
        );
120
121 5
        $dictionary = $this->createDictionary(
122 5
            $dictionaryConfig['class'] ?? $config->getDefaultDictionaryClass()
123
        );
124
125 3
        $dictionary->setName($name);
126 3
        $dictionary->setDataProvider($dataProvider);
127 3
        $dictionary->setDefaultView($dictionaryConfig['view'] ?? ($config->getDefaultView() ?? null));
128 3
        $dictionary->setSearchFields($dictionaryConfig['searchFields'] ?? []);
129 3
        $dictionary->setDataProviderFilter($dictionaryConfig['filter'] ?? null);
130
131 3
        if (isset($dictionaryConfig['cache'])) {
132 2
            $cache = $config->getCache($dictionaryConfig['cache']);
133 2
            if (!$cache) {
134 1
                throw new InvalidConfigurationException(sprintf('Cache "%s" not found', $dictionaryConfig['cache']));
135
            }
136
137 1
            $dictionary->setCache(
138 1
                $cache,
139 1
                $dictionaryConfig['cacheTTL'] ?? ConfigInterface::DEFAULT_CACHE_TTL
140
            );
141
        }
142
143 2
        return $dictionary;
144
    }
145
146
    /**
147
     * @param string $class
148
     * @param array $config
149
     *
150
     * @return DataProviderInterface
151
     * @throws InvalidConfigurationException
152
     */
153 6
    protected function createDataProvider(string $class, array $config): DataProviderInterface
154
    {
155 6
        if (!class_exists($class)) {
156 1
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class));
157
        }
158
159 5
        $dataProvider = new $class($config);
160
161 5
        if (!($dataProvider instanceof DataProviderInterface)) {
162 1
            throw new InvalidConfigurationException(sprintf('Class "%s" is not implement required interface', $class));
163
        }
164
165 4
        return $dataProvider;
166
    }
167
168
    /**
169
     * @param string $class
170
     *
171
     * @return DictionaryInterface
172
     * @throws InvalidConfigurationException
173
     */
174 4
    public function createDictionary(string $class): DictionaryInterface
175
    {
176 4
        if (!class_exists($class)) {
177 1
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class));
178
        }
179
180 3
        $dictionary = new $class;
181
182 3
        if (!($dictionary instanceof DictionaryInterface)) {
183 1
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class));
184
        }
185
186 2
        return $dictionary;
187
    }
188
}
189