CacheSettingsProvider::unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Settings;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Application\CoreSettings\CoreData;
22
use Limoncello\Application\Exceptions\AmbiguousSettingsException;
23
use Limoncello\Application\Exceptions\NotRegisteredSettingsException;
24
use Limoncello\Contracts\Application\ApplicationConfigurationInterface;
25
use Limoncello\Contracts\Application\CacheSettingsProviderInterface;
26
use ReflectionException;
27
use function array_key_exists;
28
29
/**
30
 * @package Limoncello\Application
31
 */
32
class CacheSettingsProvider implements CacheSettingsProviderInterface
33
{
34
    /** Internal data index */
35
    const KEY_APPLICATION_CONFIGURATION = 0;
36
37
    /** Internal data index */
38
    const KEY_CORE_DATA = self::KEY_APPLICATION_CONFIGURATION + 1;
39
40
    /** Internal data index */
41
    const KEY_SETTINGS_MAP = self::KEY_CORE_DATA + 1;
42
43
    /** Internal data index */
44
    const KEY_SETTINGS_DATA = self::KEY_SETTINGS_MAP + 1;
45
46
    /** Internal data index */
47
    const KEY_AMBIGUOUS_MAP = self::KEY_SETTINGS_DATA + 1;
48
49
    /**
50
     * @var array
51
     */
52
    private $appConfig = [];
53
54
    /**
55
     * @var array
56
     */
57
    private $coreData = [];
58
59
    /**
60
     * @var array
61
     */
62
    private $settingsMap = [];
63
64
    /**
65
     * @var array
66
     */
67
    private $settingsData = [];
68
69
    /**
70
     * @var array
71
     */
72
    private $ambiguousMap = [];
73
74 3
    /**
75
     * @inheritdoc
76 3
     */
77 2
    public function get(string $className): array
78 1
    {
79
        if ($this->has($className) === false) {
80 1
            if (array_key_exists($className, $this->ambiguousMap) === true) {
81
                throw new AmbiguousSettingsException($className);
82
            }
83 1
            throw new NotRegisteredSettingsException($className);
84
        }
85 1
86
        $data = $this->settingsData[$this->settingsMap[$className]];
87
88
        return $data;
89
    }
90
91 1
    /**
92
     * @inheritdoc
93 1
     */
94
    public function getApplicationConfiguration(): array
95
    {
96
        return $this->appConfig;
97
    }
98
99 2
    /**
100
     * @inheritdoc
101 2
     */
102
    public function getCoreData(): array
103
    {
104
        return $this->coreData;
105
    }
106
107 3
    /**
108
     * @inheritdoc
109 3
     */
110
    public function has(string $className): bool
111 3
    {
112
        $result = array_key_exists($className, $this->settingsMap);
113
114
        return $result;
115
    }
116
117 1
    /**
118
     * @inheritdoc
119 1
     */
120
    public function isAmbiguous(string $className): bool
121 1
    {
122
        $result = array_key_exists($className, $this->ambiguousMap);
123
124
        return $result;
125
    }
126
127
    /**
128
     * @param ApplicationConfigurationInterface $appConfig
129
     * @param CoreData                          $coreData
130
     * @param InstanceSettingsProvider          $provider
131
     *
132
     * @return self
133 6
     *
134
     * @throws ReflectionException
135
     */
136
    public function setInstanceSettings(
137
        ApplicationConfigurationInterface $appConfig,
138 6
        CoreData $coreData,
139 6
        InstanceSettingsProvider $provider
140 6
    ): self {
141 6
        $this->unserialize([
0 ignored issues
show
Unused Code introduced by
The call to the method Limoncello\Application\S...Provider::unserialize() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
142 6
            static::KEY_APPLICATION_CONFIGURATION => $appConfig->get(),
143 6
            static::KEY_CORE_DATA                 => $coreData->get(),
144
            static::KEY_SETTINGS_MAP              => $provider->getSettingsMap(),
145
            static::KEY_SETTINGS_DATA             => $provider->getSettingsData(),
146 6
            static::KEY_AMBIGUOUS_MAP             => $provider->getAmbiguousMap(),
147
        ]);
148
149
        return $this;
150
    }
151
152 2
    /**
153
     * @inheritdoc
154
     */
155 2
    public function serialize(): array
156 2
    {
157 2
        return [
158 2
            static::KEY_APPLICATION_CONFIGURATION => $this->appConfig,
159 2
            static::KEY_CORE_DATA                 => $this->coreData,
160
            static::KEY_SETTINGS_MAP              => $this->settingsMap,
161
            static::KEY_SETTINGS_DATA             => $this->settingsData,
162
            static::KEY_AMBIGUOUS_MAP             => $this->ambiguousMap,
163
        ];
164
    }
165
166 6
    /**
167
     * @inheritdoc
168
     */
169 6
    public function unserialize(array $serialized): void
170 6
    {
171 6
        [
172 6
            static::KEY_APPLICATION_CONFIGURATION => $this->appConfig,
173 6
            static::KEY_CORE_DATA                 => $this->coreData,
174
            static::KEY_SETTINGS_MAP              => $this->settingsMap,
175
            static::KEY_SETTINGS_DATA             => $this->settingsData,
176
            static::KEY_AMBIGUOUS_MAP             => $this->ambiguousMap,
177
         ] = $serialized;
178
    }
179
}
180