Completed
Pull Request — master (#129)
by Eric
257:01 queued 192:06
created

src/Event/Cookie/Jar/CookieJar.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Cookie\Jar;
13
14
use Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface;
15
use Ivory\HttpAdapter\Event\Cookie\CookieFactory;
16
use Ivory\HttpAdapter\Event\Cookie\CookieInterface;
17
use Ivory\HttpAdapter\Message\InternalRequestInterface;
18
use Ivory\HttpAdapter\Message\ResponseInterface;
19
20
/**
21
 * {@inheritdoc}
22
 *
23
 * @author GeLo <[email protected]>
24
 */
25
class CookieJar implements CookieJarInterface
26
{
27
    /** @var \Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface */
28
    private $cookieFactory;
29
30
    /** @var \Ivory\HttpAdapter\Event\Cookie\CookieInterface[] */
31
    private $cookies = array();
32
33
    /**
34
     * Creates a cookie jar.
35
     *
36
     * @param \Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface|null $cookieFactory The cookie factory.
37
     */
38 817
    public function __construct(CookieFactoryInterface $cookieFactory = null)
39
    {
40 817
        $this->setCookieFactory($cookieFactory ?: new CookieFactory());
41 817
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 266
    public function getCookieFactory()
47
    {
48 266
        return $this->cookieFactory;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 817
    public function setCookieFactory(CookieFactoryInterface $cookieFactory)
55
    {
56 817
        $this->cookieFactory = $cookieFactory;
57 817
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 19
    public function clean()
63
    {
64 19
        foreach ($this->cookies as $cookie) {
65 19
            if ($cookie->isExpired()) {
66 19
                $this->removeCookie($cookie);
67 18
            }
68 18
        }
69 19
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 437
    public function clear($domain = null, $path = null, $name = null)
75
    {
76 437
        foreach ($this->cookies as $cookie) {
77 133
            if ($domain !== null && !$cookie->matchDomain($domain)) {
78 19
                continue;
79
            }
80
81 133
            if ($path !== null && !$cookie->matchPath($path)) {
82 19
                continue;
83
            }
84
85 133
            if ($name !== null && $cookie->getName() !== $name) {
86 19
                continue;
87
            }
88
89 133
            $this->removeCookie($cookie);
90 414
        }
91 437
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 399
    public function hasCookies()
97
    {
98 399
        return !empty($this->cookies);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 703
    public function getCookies()
105
    {
106 703
        return $this->cookies;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->cookies; (Ivory\HttpAdapter\Event\Cookie\CookieInterface[]) is incompatible with the return type declared by the interface Ivory\HttpAdapter\Event\...arInterface::getCookies of type Ivory\HttpAdapter\Event\...ookieFactoryInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 247
    public function setCookies(array $cookies)
113
    {
114 247
        $this->clear();
115 247
        $this->addCookies($cookies);
116 247
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 247
    public function addCookies(array $cookies)
122
    {
123 247
        foreach ($cookies as $cookie) {
124 247
            $this->addCookie($cookie);
125 234
        }
126 247
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 19
    public function removeCookies(array $cookies)
132
    {
133 19
        foreach ($cookies as $cookie) {
134 19
            $this->removeCookie($cookie);
135 18
        }
136 19
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 551
    public function hasCookie(CookieInterface $cookie)
142
    {
143 551
        return array_search($cookie, $this->cookies, true) !== false;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 551
    public function addCookie(CookieInterface $cookie)
150
    {
151 551
        if (!$cookie->hasName() || $this->hasCookie($cookie)) {
152 19
            return;
153
        }
154
155 532
        if (!$cookie->hasValue()) {
156 19
            $this->clear(
157 19
                $cookie->getAttribute(CookieInterface::ATTR_DOMAIN),
158 19
                $cookie->getAttribute(CookieInterface::ATTR_PATH),
159 19
                $cookie->getName()
160 18
            );
161
162 19
            return;
163
        }
164
165 532
        foreach ($this->cookies as $jarCookie) {
166 399
            if (!$cookie->compare($jarCookie)) {
167 342
                continue;
168
            }
169
170 57
            if ($cookie->getExpires() > $jarCookie->getExpires()) {
171 19
                $this->removeCookie($jarCookie);
172 19
                continue;
173
            }
174
175 38
            if ($cookie->getValue() !== $jarCookie->getValue()) {
176 19
                $this->removeCookie($jarCookie);
177 19
                continue;
178
            }
179
180 19
            return;
181 504
        }
182
183 532
        $this->cookies[] = $cookie;
184 532
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189 228
    public function removeCookie(CookieInterface $cookie)
190
    {
191 228
        unset($this->cookies[array_search($cookie, $this->cookies, true)]);
192 228
        $this->cookies = array_values($this->cookies);
193 228
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 19
    public function populate(InternalRequestInterface $request)
199
    {
200 19
        foreach ($this->cookies as $cookie) {
201 19
            if (!$cookie->isExpired() && $cookie->match($request)) {
202 19
                $request = $request->withAddedHeader('Cookie', (string) $cookie);
203 18
            }
204 18
        }
205
206 19
        return $request;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 19
    public function extract(InternalRequestInterface $request, ResponseInterface $response)
213
    {
214 19
        foreach ($response->getHeader('Set-Cookie') as $header) {
215 19
            $cookie = $this->cookieFactory->parse($header);
216
217 19
            if (!$cookie->hasAttribute(CookieInterface::ATTR_DOMAIN)) {
218 19
                $cookie->setAttribute(CookieInterface::ATTR_DOMAIN, $request->getUri()->getHost());
219 18
            }
220
221 19
            $this->addCookie($cookie);
222 18
        }
223 19
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 342
    public function count()
229
    {
230 342
        return count($this->cookies);
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 342
    public function getIterator()
237
    {
238 342
        return new \ArrayIterator($this->cookies);
239
    }
240
}
241