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

PersonalSettingsManager::getCategoryMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Personal;
13
14
use Stash\Pool;
15
use ONGR\SettingsBundle\Settings\Personal\SettingsStructure;
16
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use ONGR\SettingsBundle\Event\SettingChangeEvent;
20
21
/**
22
 * Service responsible as a gateway to user settings.
23
 */
24
class PersonalSettingsManager
25
{
26
    /**
27
     * @var string
28
     */
29
    const ROLE_GRANTED = 'ROLE_ADMIN';
30
31
    /**
32
     * @var string
33
     */
34
    const STASH_NAME = 'ongr_settings';
35
36
    /**
37
     * @var EventDispatcherInterface
38
     */
39
    protected $eventDispatcher;
40
41
    /**
42
     * @var string
43
     */
44
    private $userParam;
45
46
    /**
47
     * Stash name for the current user
48
     *
49
     * @var string
50
     */
51
    private $stash;
52
53
    /**
54
     * @var AuthorizationChecker
55
     */
56
    private $securityContext;
57
58
    /**
59
     * @var TokenStorage
60
     */
61
    private $token;
62
63
    /**
64
     * @var SettingsStructure
65
     */
66
    private $settingsStructure;
67
68
    /**
69
     * @var array;
70
     */
71
    private $userSettings = [];
72
73
    /**
74
     * Stash for storing personal settings
75
     *
76
     * @var \Pool
77
     */
78
    private $pool;
79
80
    /**
81
     * @param String                   $userParam
82
     * @param SettingsStructure        $settingsStructure
83
     * @param EventDispatcherInterface $eventDispatcher
84
     * @param AuthorizationChecker     $authorization
85
     * @param TokenStorage             $token
86
     * @param Pool                     $pool
87
     */
88
    public function __construct($userParam, $settingsStructure, $eventDispatcher, $authorization, $token, $pool)
89
    {
90
        $this->settingsStructure = $settingsStructure;
91
        $this->eventDispatcher = $eventDispatcher;
92
        $this->securityContext = $authorization;
93
        $this->token = $token;
94
        $this->pool = $pool;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pool of type object<Stash\Pool> is incompatible with the declared type object<Pool> of property $pool.

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...
95
        $this->userParam = $userParam;
96
    }
97
98
    /**
99
     * Sets settings for the current user from stash
100
     */
101
    public function setSettingsFromStash()
102
    {
103
        $this->stash = $this->getStashName();
104
        $stashedSettings = $this->pool->getItem($this->stash)->get();
105
        if (is_array($stashedSettings)) {
106
            $this->userSettings = $stashedSettings;
107
        }
108
    }
109
110
    /**
111
     * @param array $settings
112
     */
113
    public function setSettings(array $settings)
114
    {
115
        $this->userSettings = $settings;
116
    }
117
118
    /**
119
     * If user logged in, returns setting value from cookie. Else, returns false.
120
     *
121
     * @param string $settingName
122
     *
123
     * @return bool
124
     */
125
    public function getSettingEnabled($settingName)
126
    {
127
        $this->setSettingsFromStash();
128
        if (isset($this->userSettings[$settingName])) {
129
            return $this->userSettings[$settingName];
130
        }
131
132
        return false;
133
    }
134
135
    /**
136
     * Saves the current settings to stash
137
     *
138
     * @throws \BadMethodCallException
139
     */
140
    public function save()
141
    {
142
        $stashedSettings = $this->pool->getItem($this->stash);
143
        $stashedSettings->set($this->userSettings);
144
        $this->pool->save($stashedSettings);
145
146
        $this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('save'));
0 ignored issues
show
Documentation introduced by
'save' is of type string, but the function expects a object<ONGR\SettingsBundle\Event\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
    }
148
149
    /**
150
     * Clears the stash
151
     */
152
    public function stashClear()
153
    {
154
        $this->pool->deleteItem($this->stash);
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function isAuthenticated()
161
    {
162
        return $this->securityContext->isGranted(self::ROLE_GRANTED);
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function getSettings()
169
    {
170
        return $this->userSettings;
171
    }
172
173
    /**
174
     * Gets active profiles
175
     *
176
     * @return array
177
     */
178
    public function getActiveProfiles()
179
    {
180
        $profiles = [];
181
        $this->setSettingsFromStash();
182
        foreach ($this->userSettings as $name => $userSetting) {
183
            if (preg_match('/^ongr_settings_profile_.*/', $name) && $userSetting) {
184
                $escapedProfile = mb_substr($name, 22, null, 'UTF-8');
185
                $profiles[] = UnderscoreEscaper::unescape($escapedProfile);
186
            }
187
        }
188
        return $profiles;
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function getSettingsMap()
195
    {
196
        return $this->settingsStructure->getStructure();
197
    }
198
199
    /**
200
     * @return array
201
     */
202
    public function getCategoryMap()
203
    {
204
        return $this->settingsStructure->getCategoriesStructure();
205
    }
206
207
    /**
208
     * Returns the full name of the stash for the current
209
     * User. If the unique user property value cant be determined
210
     * returns false.
211
     *
212
     * @return string
213
     */
214
    private function getStashName()
215
    {
216
        return self::STASH_NAME.'_'.$this->getUsername();
217
    }
218
219
    /**
220
     * Gets the defined unique user setting value
221
     *
222
     * @return string
223
     * @throws \BadMethodCallException
224
     */
225
    public function getUsername()
226
    {
227
        $user = $this->token->getToken()->getUser();
228
        $call = $this->guessPropertyOrMethodName($user);
229
        try {
230
            if ($call[0] == 'public') {
231
                return $user->$call[1];
232
            } else {
233
                $method = $call[1];
234
                if ($user == 'anon.') {
235
                    return $user;
236
                } else {
237
                    return $user->$method();
238
                }
239
            }
240
        } catch (\Exception $e) {
241
            throw new \BadMethodCallException(
242
                'Ongr could not guess the getter method for your defined user property.'
243
            );
244
        }
245
    }
246
247
    /**
248
     * Returns the property visibility and if its
249
     * private, guesses the name of the getter
250
     *
251
     * @param $user
252
     *
253
     * @return array
254
     */
255
    private function guessPropertyOrMethodName($user)
256
    {
257
        $property = $this->userParam;
258
        if (isset($user->$property)) {
259
            return ['public', $this->userParam];
260
        } else {
261
            return ['private', $this->toCamelCase($this->userParam)];
262
        }
263
    }
264
265
    /**
266
     * Converts a string to camel case
267
     *
268
     * @param string $name
269
     *
270
     * @return string
271
     */
272
    private function toCamelCase($name)
273
    {
274
        $return = explode('_', $name);
275
        foreach ($return as $key => $item) {
276
            $return[$key] = ucfirst($item);
277
        }
278
        return 'get'.implode('', $return);
279
    }
280
}
281