CookieInjector   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 95
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setCookieModels() 0 4 1
A addCookieModel() 0 4 1
A getCookieModels() 0 4 1
A inject() 0 7 2
A getHttpCookie() 0 12 3
B update() 0 24 6
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\CookiesBundle\Cookie\Service;
13
14
use ONGR\CookiesBundle\Cookie\Model\CookieInterface;
15
use Symfony\Component\HttpFoundation\Cookie;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * Injects HTTP cookie into cookie models.
21
 */
22
class CookieInjector
23
{
24
    /**
25
     * @var CookieInterface[]
26
     */
27
    protected $cookieModels = [];
28
29
    /**
30
     * Inject HTTP cookies into cookie models.
31
     *
32
     * @param Request $request
33
     */
34
    public function inject(Request $request)
35
    {
36
        foreach ($this->cookieModels as $cookieModel) {
37
            $cookie = $this->getHttpCookie($cookieModel, $request);
38
            $cookieModel->load($cookie);
0 ignored issues
show
Documentation introduced by
$cookie is of type array|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
        }
40
    }
41
42
    /**
43
     * Get HTTP cookie for $model.
44
     *
45
     * @param CookieInterface $cookieModel
46
     * @param Request         $request
47
     *
48
     * @return array|null
49
     */
50
    protected function getHttpCookie(CookieInterface $cookieModel, Request $request)
51
    {
52
        $modelName = $cookieModel->getName();
53
54
        if (null === $request->cookies || !$request->cookies->has($modelName)) {
55
            return null;
56
        }
57
58
        $cookie = $request->cookies->get($modelName);
59
60
        return $cookie;
61
    }
62
63
    /**
64
     * @param \ONGR\CookiesBundle\Cookie\Model\CookieInterface[] $cookieModels
65
     */
66
    public function setCookieModels(array $cookieModels)
67
    {
68
        $this->cookieModels = $cookieModels;
69
    }
70
71
    /**
72
     * @param CookieInterface $cookieModel
73
     */
74
    public function addCookieModel(CookieInterface $cookieModel)
75
    {
76
        $this->cookieModels[] = $cookieModel;
77
    }
78
79
    /**
80
     * @return \ONGR\CookiesBundle\Cookie\Model\CookieInterface[]
81
     */
82
    public function getCookieModels()
83
    {
84
        return $this->cookieModels;
85
    }
86
87
    /**
88
     * Add modified or removed cookies to the response object.
89
     *
90
     * @param Response $response
91
     */
92
    public function update(Response $response)
93
    {
94
        $flatCookies = $response->headers->getCookies();
95
        $cookies = [];
96
97
        $getCookieId = function (Cookie $cookie) {
98
            return $cookie->getDomain() . '|' . $cookie->getPath() . '|' . $cookie->getName();
99
        };
100
101
        /** @var Cookie $cookie */
102
        foreach ($flatCookies as $cookie) {
103
            $cookies[$getCookieId($cookie)] = $cookie;
104
        }
105
106
        foreach ($this->cookieModels as $cookieModel) {
107
            $cookie = $cookieModel->toCookie();
108
109
            if ($cookieModel->getClear()) {
110
                $response->headers->clearCookie($cookie->getName(), $cookie->getPath(), $cookie->getDomain());
111
            } elseif (!isset($cookies[$getCookieId($cookie)]) && $cookieModel->isDirty()) {
112
                $response->headers->setCookie($cookieModel->toCookie());
113
            }
114
        }
115
    }
116
}
117