Passed
Push — master ( 3bddf7...37f0e3 )
by Max
07:30
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
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 EasyDictionary\Interfaces\ConfigInterface;
10
use EasyDictionary\Interfaces\DataProviderInterface;
11
use EasyDictionary\Interfaces\DictionaryInterface;
12
13
/**
14
 * Class Manager
15
 *
16
 * @package EasyDictionary
17
 */
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...
18
class Manager
19
{
20
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
     * @var ConfigInterface
22
     */
23
    protected $config;
24
25
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @var array
27
     */
28
    protected $dictionaries = [];
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @param ConfigInterface $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
32
     */
33
    public function __construct(ConfigInterface $config = null)
34
    {
35
        if (!is_null($config)) {
36
            $this->setConfig($config);
37
        }
38
    }
39
40
    /**
41
     * Returns Dictionary object
42
     *
43
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     *
45
     * @return DictionaryInterface
46
     * @throws InvalidConfigurationException
47
     * @throws RuntimeException
48
     */
49
    public function get(string $name): ?DictionaryInterface
50
    {
51
        if (!isset($this->dictionaries[$name])) {
52
            $config = $this->getConfig();
53
            if (!$config) {
54
                throw new RuntimeException(sprintf('Config not found', $name));
55
            }
56
57
            $dictionaryConfig = $config->getDictionaryConfig()[$name] ?? null;
58
            if (!$dictionaryConfig) {
59
                throw new RuntimeException(sprintf('Dictionary with key "%s" not found', $name));
60
            }
61
62
            $this->add($this->create($name, $dictionaryConfig));
63
        }
64
65
        return $this->dictionaries[$name] ?? null;
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
69
     * @return ConfigInterface
70
     */
71
    public function getConfig(): ?ConfigInterface
72
    {
73
        return $this->config;
74
    }
75
76
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
77
     * @param ConfigInterface $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     *
79
     * @return $this
80
     */
81
    public function setConfig(ConfigInterface $config)
82
    {
83
        $this->config = $config;
84
85
        return $this;
86
    }
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
89
     * @param DictionaryInterface $dictionary
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
90
     *
91
     * @return $this
92
     * @throws RuntimeException
93
     */
94
    public function add(DictionaryInterface $dictionary)
95
    {
96
        $name = $dictionary->getName();
97
98
        if (isset($this->dictionaries[$name])) {
99
            throw new RuntimeException(sprintf('The dictionary with key "%s" already exists', $name));
100
        }
101
102
        $this->dictionaries[$name] = $dictionary;
103
104
        return $this;
105
    }
106
107
    /**
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...
108
     * @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...
109
     * @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...
110
     * @return DictionaryInterface
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
111
     * @throws InvalidConfigurationException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
112
     */
113
    protected function create(string $name, array $dictionaryConfig): DictionaryInterface
114
    {
115
        $config = $this->getConfig();
116
117
        $dataProvider = $this->createDataProvider(
118
            $dictionaryConfig['data']['class'] ?? $config->getDefaultDataProviderClass(),
119
            $dictionaryConfig['data'] ?? []
120
        );
121
122
        $dictionary = $this->createDictionary(
123
            $dictionaryConfig['class'] ?? $config->getDefaultDictionaryClass()
124
        );
125
126
        $dictionary->setName($name);
127
        $dictionary->setDataProvider($dataProvider);
128
        $dictionary->setDefaultView($dictionaryConfig['view'] ?? ($config->getDefaultView() ?? null));
129
        $dictionary->setSearchFields($dictionaryConfig['searchFields'] ?? []);
130
131
        if (isset($dictionaryConfig['cache'])) {
132
            $dictionary->setCache(
133
                $config->getCache($dictionaryConfig['cache']),
0 ignored issues
show
Bug introduced by
It seems like $config->getCache($dictionaryConfig['cache']) can be null; however, setCache() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
134
                $dictionaryConfig['cacheTTL'] ?? ConfigInterface::DEFAULT_CACHE_TTL
135
            );
136
        }
137
138
        return $dictionary;
139
    }
140
141
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
142
     * @param string $class
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
143
     * @param array $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
144
     *
145
     * @return DataProviderInterface
146
     * @throws InvalidConfigurationException
147
     */
148
    protected function createDataProvider(string $class, array $config): DataProviderInterface
149
    {
150
        if (!class_exists($class)) {
151
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class));
152
        }
153
154
        $dataProvider = new $class($config);
155
156
        if (!($dataProvider instanceof DataProviderInterface)) {
157
            throw new InvalidConfigurationException(sprintf('Class "%s" is not implement required interface', $class));
158
        }
159
160
        return $dataProvider;
161
    }
162
163
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
164
     * @param string $class
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
165
     *
166
     * @return DictionaryInterface
167
     * @throws InvalidConfigurationException
168
     */
169
    public function createDictionary(string $class): DictionaryInterface
170
    {
171
        if (!class_exists($class)) {
172
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class));
173
        }
174
175
        $dictionary = new $class;
176
177
        if (!($dictionary instanceof DictionaryInterface)) {
178
            throw new InvalidConfigurationException(sprintf('Class "%s" not found', $class));
179
        }
180
181
        return $dictionary;
182
    }
183
}
184