Completed
Pull Request — master (#168)
by
unknown
02:49
created

SettingsContainer::get()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 40
rs 5.3846
cc 8
eloc 23
nc 16
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
        $user = $this->manager->getUsername();
106
        $cachedSetting = $this->getCache($user);
107
108
        if (!$cachedSetting->isMiss()) {
109
            $cacheSettings = $cachedSetting->get();
110
            if (key($cacheSettings) == $user) {
111
                $this->settings = json_decode($cacheSettings[$user], true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($cacheSettings[$user], 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...
112
            }
113
114
            return $this->getSetting($setting);
115
        }
116
117
        $settings = [];
118
119
        foreach ($this->providers as $provider) {
120
            if (in_array($provider->getProfile(), $this->profiles)) {
121
                $settings = array_merge($settings, $provider->getSettings());
122
            }
123
        }
124
125
        $cachedSetting->set([$user => json_encode($settings)]);
126
        $this->pool->save($cachedSetting);
127
        $this->settings = array_merge($this->settings, $settings);
128
129
        return $this->getSetting($setting);
130
    }
131
132
    /**
133
     * Handles setting change event.
134
     *
135
     * @param SettingChangeEvent $event
136
     */
137
    public function onSettingChange(
138
        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...
139
    ) {
140
        $user = $this->manager->getUsername();
141
        $this->pool->getItem('ongr_settings.settings_cache_'.$user)->clear();
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function getProfiles()
148
    {
149
        return $this->profiles;
150
    }
151
152
    /**
153
     * @param array $profiles
154
     */
155
    public function setProfiles($profiles)
156
    {
157
        $this->profiles = $profiles;
158
    }
159
160
    /**
161
     * @param string $profile
162
     */
163
    public function addProfile($profile)
164
    {
165
        $this->profiles[] = $profile;
166
    }
167
168
    /**
169
     * Returns settings cache item.
170
     *
171
     * @param string $user
172
     *
173
     * @return ItemInterface
174
     */
175
    protected function getCache($user)
176
    {
177
        return $this->pool->getItem('ongr_settings.settings_cache_'.$user);
178
    }
179
180
    /**
181
     * Returns setting value.
182
     *
183
     * @param string $setting
184
     * @param bool   $throwException
185
     *
186
     * @return mixed
187
     *
188
     * @throws SettingNotFoundException
189
     */
190
    protected function getSetting($setting, $throwException = true)
191
    {
192
        if (array_key_exists($setting, $this->settings)) {
193
            return $this->settings[$setting];
194
        } elseif ($throwException) {
195
            throw new SettingNotFoundException("Setting '{$setting}' does not exist.");
196
        }
197
198
        return null;
199
    }
200
201
    /**
202
     * BuildProvider.
203
     *
204
     * @param string $profile
205
     *
206
     * @return ManagerAwareSettingProvider
207
     */
208
    private function buildProvider($profile)
209
    {
210
        $provider = new ManagerAwareSettingProvider($profile);
211
        $provider->setManager($this->esManager);
212
213
        return $provider;
214
    }
215
}
216