Completed
Push — master ( 81150b...194baa )
by Eric
02:26
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
    public function __construct(CookieFactoryInterface $cookieFactory = null)
39
    {
40
        $this->setCookieFactory($cookieFactory ?: new CookieFactory());
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getCookieFactory()
47
    {
48
        return $this->cookieFactory;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function setCookieFactory(CookieFactoryInterface $cookieFactory)
55
    {
56
        $this->cookieFactory = $cookieFactory;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function clean()
63
    {
64
        foreach ($this->cookies as $cookie) {
65
            if ($cookie->isExpired()) {
66
                $this->removeCookie($cookie);
67
            }
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function clear($domain = null, $path = null, $name = null)
75
    {
76
        foreach ($this->cookies as $cookie) {
77
            if ($domain !== null && !$cookie->matchDomain($domain)) {
78
                continue;
79
            }
80
81
            if ($path !== null && !$cookie->matchPath($path)) {
82
                continue;
83
            }
84
85
            if ($name !== null && $cookie->getName() !== $name) {
86
                continue;
87
            }
88
89
            $this->removeCookie($cookie);
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function hasCookies()
97
    {
98
        return !empty($this->cookies);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getCookies()
105
    {
106
        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
    public function setCookies(array $cookies)
113
    {
114
        $this->clear();
115
        $this->addCookies($cookies);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function addCookies(array $cookies)
122
    {
123
        foreach ($cookies as $cookie) {
124
            $this->addCookie($cookie);
125
        }
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function removeCookies(array $cookies)
132
    {
133
        foreach ($cookies as $cookie) {
134
            $this->removeCookie($cookie);
135
        }
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function hasCookie(CookieInterface $cookie)
142
    {
143
        return array_search($cookie, $this->cookies, true) !== false;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function addCookie(CookieInterface $cookie)
150
    {
151
        if (!$cookie->hasName() || $this->hasCookie($cookie)) {
152
            return;
153
        }
154
155
        if (!$cookie->hasValue()) {
156
            $this->clear(
157
                $cookie->getAttribute(CookieInterface::ATTR_DOMAIN),
158
                $cookie->getAttribute(CookieInterface::ATTR_PATH),
159
                $cookie->getName()
160
            );
161
162
            return;
163
        }
164
165
        foreach ($this->cookies as $jarCookie) {
166
            if (!$cookie->compare($jarCookie)) {
167
                continue;
168
            }
169
170
            if ($cookie->getExpires() > $jarCookie->getExpires()) {
171
                $this->removeCookie($jarCookie);
172
                continue;
173
            }
174
175
            if ($cookie->getValue() !== $jarCookie->getValue()) {
176
                $this->removeCookie($jarCookie);
177
                continue;
178
            }
179
180
            return;
181
        }
182
183
        $this->cookies[] = $cookie;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function removeCookie(CookieInterface $cookie)
190
    {
191
        unset($this->cookies[array_search($cookie, $this->cookies, true)]);
192
        $this->cookies = array_values($this->cookies);
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function populate(InternalRequestInterface $request)
199
    {
200
        foreach ($this->cookies as $cookie) {
201
            if (!$cookie->isExpired() && $cookie->match($request)) {
202
                $request = $request->withAddedHeader('Cookie', (string) $cookie);
203
            }
204
        }
205
206
        return $request;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function extract(InternalRequestInterface $request, ResponseInterface $response)
213
    {
214
        foreach ($response->getHeader('Set-Cookie') as $header) {
215
            $cookie = $this->cookieFactory->parse($header);
216
217
            if (!$cookie->hasAttribute(CookieInterface::ATTR_DOMAIN)) {
218
                $cookie->setAttribute(CookieInterface::ATTR_DOMAIN, $request->getUri()->getHost());
219
            }
220
221
            $this->addCookie($cookie);
222
        }
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function count()
229
    {
230
        return count($this->cookies);
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function getIterator()
237
    {
238
        return new \ArrayIterator($this->cookies);
239
    }
240
}
241