Completed
Push — master ( 146147...1776d4 )
by Neomerx
01:58
created

CacheSettingsProvider::getCoreData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Limoncello\Application\Settings;
2
3
/**
4
 * Copyright 2015-2017 [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
25
/**
26
 * @package Limoncello\Application
27
 */
28
class CacheSettingsProvider implements CacheSettingsProviderInterface
29
{
30
    /** Internal data index */
31
    const KEY_APPLICATION_CONFIGURATION = 0;
32
33
    /** Internal data index */
34
    const KEY_CORE_DATA = self::KEY_APPLICATION_CONFIGURATION + 1;
35
36
    /** Internal data index */
37
    const KEY_SETTINGS_MAP = self::KEY_CORE_DATA + 1;
38
39
    /** Internal data index */
40
    const KEY_SETTINGS_DATA = self::KEY_SETTINGS_MAP + 1;
41
42
    /** Internal data index */
43
    const KEY_AMBIGUOUS_MAP = self::KEY_SETTINGS_DATA + 1;
44
45
    /**
46
     * @var array
47
     */
48
    private $appConfig = [];
49
50
    /**
51
     * @var array
52
     */
53
    private $coreData = [];
54
55
    /**
56
     * @var array
57
     */
58
    private $settingsMap = [];
59
60
    /**
61
     * @var array
62
     */
63
    private $settingsData = [];
64
65
    /**
66
     * @var array
67
     */
68
    private $ambiguousMap = [];
69
70
    /**
71
     * @inheritdoc
72
     */
73 3
    public function get(string $className): array
74
    {
75 3
        if ($this->has($className) === false) {
76 2
            if (array_key_exists($className, $this->ambiguousMap) === true) {
77 1
                throw new AmbiguousSettingsException($className);
78
            }
79 1
            throw new NotRegisteredSettingsException($className);
80
        }
81
82 1
        $data = $this->settingsData[$this->settingsMap[$className]];
83
84 1
        return $data;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 1
    public function getApplicationConfiguration(): array
91
    {
92 1
        return $this->appConfig;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 2
    public function getCoreData(): array
99
    {
100 2
        return $this->coreData;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106 3
    public function has(string $className): bool
107
    {
108 3
        $result = array_key_exists($className, $this->settingsMap);
109
110 3
        return $result;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 1
    public function isAmbiguous(string $className): bool
117
    {
118 1
        $result = array_key_exists($className, $this->ambiguousMap);
119
120 1
        return $result;
121
    }
122
123
    /**
124
     * @param ApplicationConfigurationInterface $appConfig
125
     * @param CoreData                          $coreData
126
     * @param InstanceSettingsProvider          $provider
127
     *
128
     * @return self
129
     */
130 6
    public function setInstanceSettings(
131
        ApplicationConfigurationInterface $appConfig,
132
        CoreData $coreData,
133
        InstanceSettingsProvider $provider
134
    ): self {
135 6
        $this->unserialize([
136 6
            static::KEY_APPLICATION_CONFIGURATION => $appConfig->get(),
137 6
            static::KEY_CORE_DATA                 => $coreData->get(),
138 6
            static::KEY_SETTINGS_MAP              => $provider->getSettingsMap(),
139 6
            static::KEY_SETTINGS_DATA             => $provider->getSettingsData(),
140 6
            static::KEY_AMBIGUOUS_MAP             => $provider->getAmbiguousMap(),
141
        ]);
142
143 6
        return $this;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 2
    public function serialize(): array
150
    {
151
        return [
152 2
            static::KEY_APPLICATION_CONFIGURATION => $this->appConfig,
153 2
            static::KEY_CORE_DATA                 => $this->coreData,
154 2
            static::KEY_SETTINGS_MAP              => $this->settingsMap,
155 2
            static::KEY_SETTINGS_DATA             => $this->settingsData,
156 2
            static::KEY_AMBIGUOUS_MAP             => $this->ambiguousMap,
157
        ];
158
    }
159
160
    /**
161
     * @inheritdoc
162
     */
163 6
    public function unserialize(array $serialized): void
164
    {
165
        list (
166 6
            static::KEY_APPLICATION_CONFIGURATION => $this->appConfig,
167 6
            static::KEY_CORE_DATA => $this->coreData,
168 6
            static::KEY_SETTINGS_MAP => $this->settingsMap,
169 6
            static::KEY_SETTINGS_DATA => $this->settingsData,
170 6
            static::KEY_AMBIGUOUS_MAP => $this->ambiguousMap,
171
            ) = $serialized;
172
    }
173
}
174