Completed
Push — master ( 04748f...585c72 )
by Marco
07:11
created

CookieManager::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 = array();
31
32
    /**
33
     * Add a cookie to the stack
34
     *
35
     * @param CookieInterface $cookie
36
     *
37
     * @return static
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
    public function register(CookieInterface $cookie) {
52
        return $this->add($cookie);
53
    }
54
55
    /**
56
     * Delete a cookie from the stack
57
     *
58
     * @param CookieInterface|string $cookie
59
     *
60
     * @return static
61
     */
62 3
    public function del($cookie) {
63
64 3
        if ( empty($cookie) ) throw new CookieException("Invalid cookie object or name");
65
66 3
        $name = ($cookie instanceof CookieInterface) ? $cookie->getName() : $cookie;
67
68 3
        if ( $this->isRegistered($name) ) unset($this->cookies[$name]);
0 ignored issues
show
Deprecated Code introduced by
The method Comodojo\Cookies\CookieManager::isRegistered() has been deprecated with message: 2.1.0

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

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

Loading history...
69
70
        else throw new CookieException("Cookie is not registered");
71
72 3
        return $this;
73
74
    }
75
76
    /**
77
     * @deprecated 2.1.0
78
     * @see CookieManager::del()
79
     */
80
    public function unregister($cookie) {
81
        return $this->del($cookie);
82
    }
83
84
    /**
85
     * Check if cookie is into the stack
86
     *
87
     * @param CookieInterface|string $cookie
88
     *
89
     * @return static
90
     */
91 3
    public function has($cookie) {
92
93 3
        if ( empty($cookie) ) throw new CookieException("Invalid cookie object or name");
94
95 3
        $name = ($cookie instanceof CookieInterface) ? $cookie->getName() : $cookie;
96
97 3
        return array_key_exists($name, $this->cookies);
98
99
    }
100
101
    /**
102
     * @deprecated 2.1.0
103
     * @see CookieManager::has()
104
     */
105 1
    public function isRegistered($cookie) {
106 1
        return $this->has($cookie);
107
    }
108
109
    /**
110
     * Get cookie from $cookie_name
111
     *
112
     * @param string $cookie_name
113
     *
114
     * @return CookieInterface
115
     */
116 3
    public function get($cookie_name) {
117
118 3
        if ( $this->isRegistered($cookie_name) ) return $this->cookies[$cookie_name];
0 ignored issues
show
Deprecated Code introduced by
The method Comodojo\Cookies\CookieManager::isRegistered() has been deprecated with message: 2.1.0

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

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

Loading history...
119
120
        else throw new CookieException("Cookie is not registered");
121
122
    }
123
124
    /**
125
     * Get the whole cookies' archive
126
     *
127
     * @return array
128
     */
129
    public function getAll() {
130
131
        return $this->cookies;
132
133
    }
134
135
    /**
136
     * Get values from all registered cookies and dump as an associative array
137
     *
138
     * @return  array
139
     */
140 3
    public function getValues() {
141
142 3
        $cookies = array();
143
144
        try {
145
146 3
            foreach ( $this->cookies as $name=>$cookie ) {
147
148 3
                $cookies[$name] = $cookie->getValue();
149
150 3
            }
151
152 3
        } catch (CookieException $ce) {
153
154
            throw $ce;
155
156
        }
157
158 3
        return $cookies;
159
160
    }
161
162
    /**
163
     * Save all registered cookies
164
     *
165
     * @return static
166
     */
167
    public function save() {
168
169
        try {
170
171
            foreach ( $this->cookies as $c ) {
172
173
                $c->save();
174
175
            }
176
177
        } catch (CookieException $ce) {
178
179
            throw $ce;
180
181
        }
182
183
        return true;
184
185
    }
186
187
    /**
188
     * Load all registered cookies
189
     *
190
     * @return static
191
     */
192
    public function load() {
193
194
        try {
195
196
            foreach ( $this->cookies as $c ) {
197
198
                $c->load();
199
200
            }
201
202
        } catch (CookieException $ce) {
203
204
            throw $ce;
205
206
        }
207
208
        return $this;
209
210
    }
211
212
}
213