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

PersonalSettingsManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 15
c 5
b 0
f 0
lcom 2
cbo 1
dl 0
loc 145
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A setSettingsFromStash() 0 4 1
A addSettingsFromStash() 0 4 1
A setSettingsFromForm() 0 4 1
A getSettingEnabled() 0 12 4
A save() 0 6 1
A stashClear() 0 4 1
A isAuthenticated() 0 4 1
A getSettings() 0 4 1
A getSettingsMap() 0 4 1
A getCategoryMap() 0 4 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\Personal;
13
14
use Symfony\Component\Security\Core\SecurityContextInterface;
15
use ONGR\SettingsBundle\Settings\Personal\SettingsStructure;
16
17
/**
18
 * Service responsible as a gateway to user settings.
19
 */
20
class PersonalSettingsManager
21
{
22
    /**
23
     * @var string
24
     */
25
    const ROLE_GRANTED = 'ROLE_ADMIN';
26
27
    /**
28
     * @var string
29
     */
30
    const STASH_NAME = 'ongr_settings';
31
32
    /**
33
     * @var SecurityContextInterface
34
     */
35
    private $securityContext;
36
37
    /**
38
     * @var SettingsStructure
39
     */
40
    private $settingsStructure;
41
42
    /**
43
     * @var array;
44
     */
45
    private $userSettings = [];
46
47
    /**
48
     * Stash for storing personal settings
49
     *
50
     * @var \Pool
51
     */
52
    private $pool;
53
54
    /**
55
     * @param SettingsStructure        $settingsStructure
56
     * @param                          $security
57
     * @param \Pool                    $pool
58
     */
59
    public function __construct($settingsStructure, $security, $pool)
60
    {
61
        $this->settingsStructure = $settingsStructure;
62
        $this->securityContext = $security;
63
        $this->pool = $pool;
64
        $stashedSettings = $pool->getItem(self::STASH_NAME)->get();
65
        if (is_array($stashedSettings)) {
66
            $this->userSettings = $stashedSettings;
67
        }
68
    }
69
70
    /**
71
     * @param array $stash
72
     */
73
    public function setSettingsFromStash(array $stash)
74
    {
75
        $this->userSettings = $stash;
76
    }
77
78
    /**
79
     * @param array $stash
80
     */
81
    public function addSettingsFromStash(array $stash)
82
    {
83
        $this->userSettings = array_merge($this->userSettings, $stash);
84
    }
85
86
    /**
87
     * @param array $settings
88
     */
89
    public function setSettingsFromForm(array $settings)
90
    {
91
        $this->userSettings = $settings;
92
    }
93
94
    /**
95
     * If user logged in, returns setting value from cookie. Else, returns false.
96
     *
97
     * @param string $settingName
98
     * @param bool   $mustAuthorize
99
     *
100
     * @return bool
101
     */
102
    public function getSettingEnabled($settingName, $mustAuthorize = true)
103
    {
104
        if ($mustAuthorize && !$this->isAuthenticated()) {
105
            return false;
106
        }
107
108
        if (isset($this->userSettings[$settingName])) {
109
            return $this->userSettings[$settingName];
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * Saves the current settings to stash
117
     */
118
    public function save()
119
    {
120
        $stashedSettings = $this->pool->getItem(self::STASH_NAME);
121
        $stashedSettings->set($this->userSettings);
122
        $this->pool->save($stashedSettings);
123
    }
124
125
    /**
126
     * Clears the stash
127
     */
128
    public function stashClear()
129
    {
130
        $this->pool->deleteItem(self::STASH_NAME);
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    public function isAuthenticated()
137
    {
138
        return $this->securityContext->isGranted(self::ROLE_GRANTED);
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getSettings()
145
    {
146
        return $this->userSettings;
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getSettingsMap()
153
    {
154
        return $this->settingsStructure->getStructure();
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function getCategoryMap()
161
    {
162
        return $this->settingsStructure->getCategoriesStructure();
163
    }
164
}
165