Completed
Push — master ( 95e117...43f948 )
by David
02:23
created

CookieJar::hasCookies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Http\Message;
4
5
/**
6
 * Cookie Jar holds a set of Cookies.
7
 *
8
 * @author Márk Sági-Kazár <[email protected]>
9
 */
10
final class CookieJar implements \Countable, \IteratorAggregate
11
{
12
    /**
13
     * @var \SplObjectStorage
14
     */
15
    protected $cookies;
16
17 14
    public function __construct()
18
    {
19 14
        $this->cookies = new \SplObjectStorage();
20 14
    }
21
22
    /**
23
     * Checks if there is a cookie.
24
     *
25
     * @param Cookie $cookie
26
     *
27
     * @return bool
28
     */
29 12
    public function hasCookie(Cookie $cookie)
30
    {
31 12
        return $this->cookies->contains($cookie);
32
    }
33
34
    /**
35
     * Adds a cookie.
36
     *
37
     * @param Cookie $cookie
38
     */
39 12
    public function addCookie(Cookie $cookie)
40
    {
41 12
        if (!$this->hasCookie($cookie)) {
42 12
            $cookies = $this->getMatchingCookies($cookie);
43
44 12
            foreach ($cookies as $matchingCookie) {
45 3
                if ($cookie->getValue() !== $matchingCookie->getValue() || $cookie->getMaxAge() > $matchingCookie->getMaxAge()) {
46 3
                    $this->removeCookie($matchingCookie);
47
48 3
                    continue;
49
                }
50 12
            }
51
52 12
            if ($cookie->hasValue()) {
53 12
                $this->cookies->attach($cookie);
54 12
            }
55 12
        }
56 12
    }
57
58
    /**
59
     * Removes a cookie.
60
     *
61
     * @param Cookie $cookie
62
     */
63 6
    public function removeCookie(Cookie $cookie)
64
    {
65 6
        $this->cookies->detach($cookie);
66 6
    }
67
68
    /**
69
     * Returns the cookies.
70
     *
71
     * @return Cookie[]
72
     */
73 1
    public function getCookies()
74
    {
75
        $match = function ($matchCookie) {
0 ignored issues
show
Unused Code introduced by
The parameter $matchCookie 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...
76 1
            return true;
77 1
        };
78
79 1
        return $this->findMatchingCookies($match);
80
    }
81
82
    /**
83
     * Returns all matching cookies.
84
     *
85
     * @param Cookie $cookie
86
     *
87
     * @return Cookie[]
88
     */
89 12
    public function getMatchingCookies(Cookie $cookie)
90
    {
91
        $match = function ($matchCookie) use ($cookie) {
92 9
            return $matchCookie->match($cookie);
93 12
        };
94
95 12
        return $this->findMatchingCookies($match);
96
    }
97
98
    /**
99
     * Finds matching cookies based on a callable.
100
     *
101
     * @param callable $match
102
     *
103
     * @return Cookie[]
104
     */
105 12
    protected function findMatchingCookies(callable $match)
106
    {
107 12
        $cookies = [];
108
109 12
        foreach ($this->cookies as $cookie) {
110 9
            if ($match($cookie)) {
111 6
                $cookies[] = $cookie;
112 6
            }
113 12
        }
114
115 12
        return $cookies;
116
    }
117
118
    /**
119
     * Checks if there are cookies.
120
     *
121
     * @return bool
122
     */
123 6
    public function hasCookies()
124
    {
125 6
        return $this->cookies->count() > 0;
126
    }
127
128
    /**
129
     * Sets the cookies and removes any previous one.
130
     *
131
     * @param Cookie[] $cookies
132
     */
133 1
    public function setCookies(array $cookies)
134
    {
135 1
        $this->clear();
136 1
        $this->addCookies($cookies);
137 1
    }
138
139
    /**
140
     * Adds some cookies.
141
     *
142
     * @param Cookie[] $cookies
143
     */
144 5
    public function addCookies(array $cookies)
145
    {
146 5
        foreach ($cookies as $cookie) {
147 5
            $this->addCookie($cookie);
148 5
        }
149 5
    }
150
151
    /**
152
     * Removes some cookies.
153
     *
154
     * @param Cookie[] $cookies
155
     */
156 2
    public function removeCookies(array $cookies)
157
    {
158 2
        foreach ($cookies as $cookie) {
159 2
            $this->removeCookie($cookie);
160 2
        }
161 2
    }
162
163
    /**
164
     * Removes cookies which match the given parameters.
165
     *
166
     * Null means that parameter should not be matched
167
     *
168
     * @param string|null $name
169
     * @param string|null $domain
170
     * @param string|null $path
171
     */
172
    public function removeMatchingCookies($name = null, $domain = null, $path = null)
173
    {
174 1
        $match = function ($cookie) use ($name, $domain, $path) {
175 1
            $match = true;
176
177 1
            if (isset($name)) {
178 1
                $match = $match && ($cookie->getName() === $name);
179 1
            }
180
181 1
            if (isset($domain)) {
182 1
                $match = $match && $cookie->matchDomain($domain);
183 1
            }
184
185 1
            if (isset($path)) {
186 1
                $match = $match && $cookie->matchPath($path);
187 1
            }
188
189 1
            return $match;
190 1
        };
191
192 1
        $cookies = $this->findMatchingCookies($match);
193
194 1
        $this->removeCookies($cookies);
195 1
    }
196
197
    /**
198
     * Removes all cookies.
199
     */
200 2
    public function clear()
201
    {
202 2
        $this->cookies = new \SplObjectStorage();
203 2
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208 5
    public function count()
209
    {
210 5
        return $this->cookies->count();
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216 1
    public function getIterator()
217
    {
218 1
        return clone $this->cookies;
219
    }
220
}
221