Completed
Push — develop ( 62c23a...4f6165 )
by Neomerx
05:22
created

CacheSettingsProvider::getCoreData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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