Completed
Pull Request — master (#22)
by Márk
02:20
created

CookieJarTemplate::removeMatchingCookies()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 6.7273
cc 7
eloc 12
nc 1
nop 3
crap 7
1
<?php
2
3
namespace Http\Message\CookieJar;
4
5
use Http\Message\Cookie;
6
7
/**
8
 * Common implementation of a Cookie Jar.
9
 *
10
 * @author Márk Sági-Kazár <[email protected]>
11
 */
12
trait CookieJarTemplate
13
{
14
    /**
15
     * @var \SplObjectStorage
16
     */
17
    protected $cookies;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 12
    public function hasCookie(Cookie $cookie)
23
    {
24 12
        return $this->cookies->contains($cookie);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 12
    public function addCookie(Cookie $cookie)
31
    {
32 12
        if (!$this->hasCookie($cookie)) {
33 12
            $cookies = $this->getMatchingCookies($cookie);
34
35 12
            foreach ($cookies as $matchingCookie) {
36 3
                if ($cookie->getValue() !== $matchingCookie->getValue() || $cookie->getExpires() > $matchingCookie->getExpires()) {
37 3
                    $this->removeCookie($matchingCookie);
38
39 3
                    continue;
40
                }
41 12
            }
42
43 12
            if ($cookie->hasValue()) {
44 12
                $this->cookies->attach($cookie);
45 12
            }
46
47 12
        }
48 12
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    public function removeCookie(Cookie $cookie)
54
    {
55 6
        $this->cookies->detach($cookie);
56 6
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function getCookies()
62
    {
63
        $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...
64 1
            return true;
65 1
        };
66
67 1
        return $this->findMatchingCookies($match);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 12
    public function getMatchingCookies(Cookie $cookie)
74
    {
75
        $match = function($matchCookie) use ($cookie) {
76 9
            return $matchCookie->match($cookie);
77 12
        };
78
79 12
        return $this->findMatchingCookies($match);
80
    }
81
82
    /**
83
     * Finds matching cookies based on a callable
84
     *
85
     * @param callable $match
86
     *
87
     * @return Cookie[]
88
     */
89 12
    protected function findMatchingCookies(callable $match)
90
    {
91 12
        $cookies = [];
92
93 12
        foreach ($this->cookies as $cookie) {
94 9
            if ($match($cookie)) {
95 6
                $cookies[] = $cookie;
96 6
            }
97 12
        }
98
99 12
        return $cookies;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 6
    public function hasCookies()
106
    {
107 6
        return $this->cookies->count() > 0;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function setCookies(array $cookies)
114
    {
115 1
        $this->clear();
116 1
        $this->addCookies($cookies);
117 1
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 5
    public function addCookies(array $cookies)
123
    {
124 5
        foreach ($cookies as $cookie) {
125 5
            $this->addCookie($cookie);
126 5
        }
127 5
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 2
    public function removeCookies(array $cookies)
133
    {
134 2
        foreach ($cookies as $cookie) {
135 2
            $this->removeCookie($cookie);
136 2
        }
137 2
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function removeMatchingCookies($name = null, $domain = null, $path = null)
143
    {
144 1
        $match = function($cookie) use ($name, $domain, $path) {
145 1
            $match = true;
146
147 1
            if (isset($name)) {
148 1
                $match = $match && ($cookie->getName() === $name);
149 1
            }
150
151 1
            if (isset($domain)) {
152 1
                $match = $match && $cookie->matchDomain($domain);
153 1
            }
154
155 1
            if (isset($path)) {
156 1
                $match = $match && $cookie->matchPath($path);
157 1
            }
158
159 1
            return $match;
160 1
        };
161
162 1
        $cookies = $this->findMatchingCookies($match);
163
164 1
        $this->removeCookies($cookies);
165 1
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 2
    public function clear()
171
    {
172 2
        $this->cookies = new \SplObjectStorage();
173 2
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 5
    public function count()
179
    {
180 5
        return $this->cookies->count();
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 1
    public function getIterator()
187
    {
188 1
        return clone $this->cookies;
189
    }
190
}
191