CookieManager   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
dl 0
loc 216
ccs 23
cts 46
cp 0.5
rs 10
c 0
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getValues() 0 19 3
A add() 0 5 1
A register() 0 2 1
A get() 0 7 2
A unregister() 0 2 1
A isRegistered() 0 2 1
A has() 0 7 3
A load() 0 17 3
A save() 0 17 3
A getAll() 0 3 1
A del() 0 13 4
1
<?php namespace Comodojo\Cookies;
2
3
use \Comodojo\Exception\CookieException;
4
5
/**
6
 * Manage multiple cookies of different types at one time
7
 *
8
 * @package     Comodojo Spare Parts
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class CookieManager {
24
25
    /*
26
     * Cookie storage :)
27
     *
28
     * @var array
29
     */
30
    private $cookies = [];
31
32
    /**
33
     * Add a cookie to the stack
34
     *
35
     * @param CookieInterface $cookie
36
     *
37
     * @return CookieManager
38
     */
39 6
    public function add(CookieInterface $cookie) {
40
41 6
        $this->cookies[$cookie->getName()] = $cookie;
42
43 6
        return $this;
44
45
    }
46
47
    /**
48
     * @deprecated 2.1.0
49
     * @see CookieManager::add()
50
     *
51
     * Add a cookie to the stack
52
     *
53
     * @param CookieInterface $cookie
54
     *
55
     * @return CookieManager
56
     */
57
    public function register(CookieInterface $cookie) {
58
        return $this->add($cookie);
59
    }
60
61
    /**
62 3
     * Delete a cookie from the stack
63
     *
64 3
     * @param CookieInterface|string $cookie
65
     *
66 3
     * @return CookieManager
67
     * @throws CookieException
68 3
     */
69
    public function del($cookie) {
70
71
        if ( empty($cookie) ) throw new CookieException("Invalid cookie object or name");
72 3
73
        $name = ($cookie instanceof CookieInterface) ? $cookie->getName() : $cookie;
74
75
        if ( $this->isRegistered($name) ) {
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\Cookies\CookieManager::isRegistered() has been deprecated: 2.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
        if ( /** @scrutinizer ignore-deprecated */ $this->isRegistered($name) ) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
            unset($this->cookies[$name]);
77
        } else {
78
            throw new CookieException("Cookie is not registered");
79
        }
80
81
        return $this;
82
83
    }
84
85
    /**
86
     * @deprecated 2.1.0
87
     * @see CookieManager::del()
88
     *
89
     * Delete a cookie from the stack
90
     *
91 3
     * @param CookieInterface|string $cookie
92
     *
93 3
     * @return CookieManager
94
     * @throws CookieException
95 3
     */
96
    public function unregister($cookie) {
97 3
        return $this->del($cookie);
98
    }
99
100
    /**
101
     * Check if a cookie is into the stack
102
     *
103
     * @param CookieInterface|string $cookie
104
     *
105 1
     * @return CookieManager
106 1
     * @throws CookieException
107
     */
108
    public function has($cookie) {
109
110
        if ( empty($cookie) ) throw new CookieException("Invalid cookie object or name");
111
112
        $name = ($cookie instanceof CookieInterface) ? $cookie->getName() : $cookie;
113
114
        return array_key_exists($name, $this->cookies);
115
116 3
    }
117
118 3
    /**
119
     * @deprecated 2.1.0
120
     * @see CookieManager::has()
121
     *
122
     * Check if a cookie is into the stack
123
     *
124
     * @param CookieInterface|string $cookie
125
     *
126
     * @return CookieManager
127
     * @throws CookieException
128
     */
129
    public function isRegistered($cookie) {
130
        return $this->has($cookie);
131
    }
132
133
    /**
134
     * Get cookie from $cookie_name
135
     *
136
     * @param string $cookie_name
137
     *
138
     * @return CookieInterface
139
     * @throws CookieException
140 3
     */
141
    public function get($cookie_name) {
142 3
143
        if ( $this->isRegistered($cookie_name) ) {
0 ignored issues
show
Deprecated Code introduced by
The function Comodojo\Cookies\CookieManager::isRegistered() has been deprecated: 2.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

143
        if ( /** @scrutinizer ignore-deprecated */ $this->isRegistered($cookie_name) ) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
144
            return $this->cookies[$cookie_name];
145
        }
146 3
147
        throw new CookieException("Cookie is not registered");
148 3
149
    }
150 3
151
    /**
152 3
     * Get the whole cookies' archive
153
     *
154
     * @return array
155
     */
156
    public function getAll() {
157
158 3
        return $this->cookies;
159
160
    }
161
162
    /**
163
     * Get values from all registered cookies and dump as an associative array
164
     *
165
     * @return array
166
     * @throws CookieException
167
     */
168
    public function getValues() {
169
170
        $cookies = [];
171
172
        try {
173
174
            foreach ( $this->cookies as $name => $cookie ) {
175
176
                $cookies[$name] = $cookie->getValue();
177
178
            }
179
180
        } catch (CookieException $ce) {
181
182
            throw $ce;
183
184
        }
185
186
        return $cookies;
187
188
    }
189
190
    /**
191
     * Save all registered cookies
192
     *
193
     * @return CookieManager
194
     * @throws CookieException
195
     */
196
    public function save() {
197
198
        try {
199
200
            foreach ( $this->cookies as $c ) {
201
202
                $c->save();
203
204
            }
205
206
        } catch (CookieException $ce) {
207
208
            throw $ce;
209
210
        }
211
212
        return true;
213
214
    }
215
216
    /**
217
     * Load all registered cookies
218
     *
219
     * @return CookieManager
220
     * @throws CookieException
221
     */
222
    public function load() {
223
224
        try {
225
226
            foreach ( $this->cookies as $c ) {
227
228
                $c->load();
229
230
            }
231
232
        } catch (CookieException $ce) {
233
234
            throw $ce;
235
236
        }
237
238
        return $this;
239
240
    }
241
242
}
243