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

SettingsContainer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 146
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addProvider() 0 4 1
A getProfiles() 0 4 1
A setProfiles() 0 4 1
A addProfile() 0 4 1
A getCache() 0 4 1
A __construct() 0 5 1
B get() 0 29 5
A onSettingChange() 0 6 1
A getSetting() 0 10 3
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 Stash\Interfaces\ItemInterface;
17
use Stash\Interfaces\PoolInterface;
18
use ONGR\SettingsBundle\Settings\General\Provider\SettingsProviderInterface;
19
20
/**
21
 * This class provides access to application settings.
22
 */
23
class SettingsContainer implements SettingsContainerInterface
24
{
25
    /**
26
     * @var PoolInterface
27
     */
28
    protected $pool;
29
30
    /**
31
     * Array of profiles.
32
     *
33
     * Array of selected profiles / profiles to apply.
34
     *
35
     * @var array
36
     */
37
    protected $profiles;
38
39
    /**
40
     * @var SettingsProviderInterface[]
41
     */
42
    protected $providers = [];
43
44
    /**
45
     * @var array
46
     */
47
    protected $settings = [];
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param PoolInterface $pool
53
     * @param array         $profiles
54
     */
55
    public function __construct(PoolInterface $pool, $profiles = ['default'])
56
    {
57
        $this->pool = $pool;
58
        $this->profiles = $profiles;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function addProvider(SettingsProviderInterface $provider)
65
    {
66
        $this->providers[] = $provider;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function get($setting)
73
    {
74
        $result = $this->getSetting($setting, false);
75
76
        if ($result !== null) {
77
            return $result;
78
        }
79
80
        $cachedSetting = $this->getCache();
81
82
        if (!$cachedSetting->isMiss()) {
83
            $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...
84
85
            return $this->getSetting($setting);
86
        }
87
88
        $settings = [];
89
90
        foreach ($this->providers as $provider) {
91
            if (in_array($provider->getProfile(), $this->profiles)) {
92
                $settings = array_merge($settings, $provider->getSettings());
93
            }
94
        }
95
96
        $cachedSetting->set(json_encode($settings));
97
        $this->settings = array_merge($this->settings, $settings);
98
99
        return $this->getSetting($setting);
100
    }
101
102
    /**
103
     * Handles setting change event.
104
     *
105
     * @param SettingChangeEvent $event
106
     */
107
    public function onSettingChange(
108
        /** @noinspection PhpUnusedParameterInspection */
109
        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...
110
    ) {
111
        $this->pool->getItem('ongr_settings.settings_cache')->clear();
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getProfiles()
118
    {
119
        return $this->profiles;
120
    }
121
122
    /**
123
     * @param array $profiles
124
     */
125
    public function setProfiles($profiles)
126
    {
127
        $this->profiles = $profiles;
128
    }
129
130
    /**
131
     * @param string $profile
132
     */
133
    public function addProfile($profile)
134
    {
135
        $this->profiles[] = $profile;
136
    }
137
138
    /**
139
     * Returns settings cache item.
140
     *
141
     * @return ItemInterface
142
     */
143
    protected function getCache()
144
    {
145
        return $this->pool->getItem('ongr_settings.settings_cache', join($this->profiles, ','));
0 ignored issues
show
Unused Code introduced by
The call to PoolInterface::getItem() has too many arguments starting with join($this->profiles, ',').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
    }
147
148
    /**
149
     * Returns setting value.
150
     *
151
     * @param string $setting
152
     * @param bool   $throwException
153
     *
154
     * @return mixed
155
     *
156
     * @throws SettingNotFoundException
157
     */
158
    protected function getSetting($setting, $throwException = true)
159
    {
160
        if (array_key_exists($setting, $this->settings)) {
161
            return $this->settings[$setting];
162
        } elseif ($throwException) {
163
            throw new SettingNotFoundException("Setting '{$setting}' does not exist.");
164
        }
165
166
        return null;
167
    }
168
}
169