Completed
Pull Request — master (#168)
by
unknown
03:00
created

SettingsContainer::buildProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Settings\General;
13
14
use ONGR\SettingsBundle\Event\SettingChangeEvent;
15
use ONGR\SettingsBundle\Exception\SettingNotFoundException;
16
use ONGR\SettingsBundle\Settings\Personal\PersonalSettingsManager;
17
use ONGR\SettingsBundle\Settings\General\Provider\SettingsProviderInterface;
18
use ONGR\SettingsBundle\Settings\General\Provider\ManagerAwareSettingProvider;
19
use Stash\Interfaces\ItemInterface;
20
use Stash\Interfaces\PoolInterface;
21
use ONGR\ElasticsearchBundle\Service\Manager;
22
23
/**
24
 * This class provides access to application settings.
25
 */
26
class SettingsContainer implements SettingsContainerInterface
27
{
28
    /**
29
     * @var PoolInterface
30
     */
31
    private $pool;
32
33
    /**
34
     * Personal settings manager for handling
35
     * selected profiles
36
     *
37
     * @var PersonalSettingsManager
38
     */
39
    private $manager;
40
41
    /**
42
     * Elasticsearch manager for building profiders
43
     * @var Manager
44
     */
45
    private $esManager;
46
47
    /**
48
     * @var array
49
     */
50
    private $profiles = [];
51
52
    /**
53
     * @var SettingsProviderInterface[]
54
     */
55
    private $providers = [];
56
57
    /**
58
     * @var array
59
     */
60
    private $settings = [];
61
62
    /**
63
     * Constructor.
64
     *
65
     * @param PoolInterface              $pool
66
     * @param PersonalSettingsManager    $manager
67
     * @param Manager    $esManager
68
     */
69
    public function __construct(
70
        PoolInterface $pool,
71
        PersonalSettingsManager $manager,
72
        Manager $esManager
73
    )
74
    {
75
        $this->pool = $pool;
76
        $this->manager = $manager;
77
        $this->esManager = $esManager;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function addProvider(SettingsProviderInterface $provider)
84
    {
85
        $this->providers[] = $provider;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function get($setting)
92
    {
93
        $result = $this->getSetting($setting, false);
94
95
        if ($result !== null) {
96
            return $result;
97
        }
98
        $this->profiles = $this->manager->getActiveProfiles();
99
100
        foreach ($this->profiles as $profile) {
101
            if ($profile != 'default') {
102
                $this->addProvider($this->buildProvider($profile));
103
            }
104
        }
105
106
        $cachedSetting = $this->getCache();
107
108
        if (!$cachedSetting->isMiss()) {
109
            $this->settings = json_decode($cachedSetting->get(), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($cachedSetting->get(), true) of type * is incompatible with the declared type array of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
110
111
            return $this->getSetting($setting);
112
        }
113
114
        $settings = [];
115
116
        foreach ($this->providers as $provider) {
117
            if (in_array($provider->getProfile(), $this->profiles)) {
118
                $settings = array_merge($settings, $provider->getSettings());
119
            }
120
        }
121
122
        $cachedSetting->set(json_encode($settings));
123
        $this->pool->save($cachedSetting);
124
        $this->settings = array_merge($this->settings, $settings);
125
126
        return $this->getSetting($setting);
127
    }
128
129
    /**
130
     * Handles setting change event.
131
     *
132
     * @param SettingChangeEvent $event
133
     */
134
    public function onSettingChange(
135
        SettingChangeEvent $event
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    ) {
137
        $this->pool->getItem('ongr_settings.settings_cache')->clear();
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function getProfiles()
144
    {
145
        return $this->profiles;
146
    }
147
148
    /**
149
     * @param array $profiles
150
     */
151
    public function setProfiles($profiles)
152
    {
153
        $this->profiles = $profiles;
154
    }
155
156
    /**
157
     * @param string $profile
158
     */
159
    public function addProfile($profile)
160
    {
161
        $this->profiles[] = $profile;
162
    }
163
164
    /**
165
     * Returns settings cache item.
166
     *
167
     * @return ItemInterface
168
     */
169
    protected function getCache()
170
    {
171
        return $this->pool->getItem('ongr_settings.settings_cache');
172
    }
173
174
    /**
175
     * Returns setting value.
176
     *
177
     * @param string $setting
178
     * @param bool   $throwException
179
     *
180
     * @return mixed
181
     *
182
     * @throws SettingNotFoundException
183
     */
184
    protected function getSetting($setting, $throwException = true)
185
    {
186
        if (array_key_exists($setting, $this->settings)) {
187
            return $this->settings[$setting];
188
        } elseif ($throwException) {
189
            throw new SettingNotFoundException("Setting '{$setting}' does not exist.");
190
        }
191
192
        return null;
193
    }
194
195
    /**
196
     * BuildProvider.
197
     *
198
     * @param string $profile
199
     *
200
     * @return ManagerAwareSettingProvider
201
     */
202
    private function buildProvider($profile)
203
    {
204
        $provider = new ManagerAwareSettingProvider($profile);
205
        $provider->setManager($this->esManager);
206
207
        return $provider;
208
    }
209
}
210