SingleCookieStorage::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\ProfileBundle\Storage;
20
21
use DateTime;
22
use OpenConext\Profile\Assert;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
use Symfony\Component\HttpFoundation\Cookie;
25
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
26
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
27
use Symfony\Component\HttpKernel\KernelEvents;
28
29
class SingleCookieStorage implements EventSubscriberInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    private $cookieDomain;
35
36
    /**
37
     * @var string
38
     */
39
    private $cookieKey;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $cookieValue;
45
46
    /**
47
     * @var DateTime|null
48
     */
49
    private $cookieExpirationDate;
50
51
    /**
52
     * @var boolean
53
     */
54
    private $cookieSecure;
55
56
    /**
57
     * @var boolean
58
     */
59
    private $cookieHttpOnly;
60
61
    public function __construct(
62
        $cookieDomain,
63
        $cookieKey,
64
        DateTime $cookieExpirationDate = null,
65
        $cookieSecure = false,
66
        $cookieHttpOnly = true
67
    ) {
68
        Assert::string($cookieDomain, 'Cookie domain "%s" expected to be string, type %s given.');
69
        Assert::string($cookieKey, 'Cookie key "%s" expected to be string, type %s given.');
70
        Assert::boolean($cookieSecure, 'Cookie secure setting "%s" is expected to be boolean.');
71
        Assert::boolean($cookieHttpOnly, 'Cookie HttpOnly setting "%s" expected to be boolean');
72
73
        $this->cookieDomain         = $cookieDomain;
74
        $this->cookieKey            = $cookieKey;
75
        $this->cookieExpirationDate = $cookieExpirationDate;
76
        $this->cookieSecure         = $cookieSecure;
77
        $this->cookieHttpOnly       = $cookieHttpOnly;
78
    }
79
80
    /**
81
     * @param string $value
82
     */
83
    public function setValue($value)
84
    {
85
        Assert::string($value);
86
87
        $this->cookieValue = $value;
88
    }
89
90
    /**
91
     * @return string|null
92
     */
93
    public function getValue()
94
    {
95
        return $this->cookieValue;
96
    }
97
98
    /**
99
     * @param GetResponseEvent $event
100
     */
101
    public function loadValueFromCookie(GetResponseEvent $event)
102
    {
103
        $this->cookieValue = $event->getRequest()->cookies->get($this->cookieKey, null);
104
    }
105
106
    /**
107
     * @param FilterResponseEvent $event
108
     */
109
    public function storeValueInCookie(FilterResponseEvent $event)
110
    {
111
        // If no date is specified for cookie expiration, a session cookie should be created
112
        $cookieExpirationDate = $this->cookieExpirationDate ?: 0;
113
114
        $event->getResponse()->headers->setCookie(new Cookie(
115
            $this->cookieKey,
116
            $this->cookieValue,
117
            $cookieExpirationDate,
0 ignored issues
show
Bug introduced by
It seems like $cookieExpirationDate defined by $this->cookieExpirationDate ?: 0 on line 112 can also be of type object<DateTime>; however, Symfony\Component\HttpFo...n\Cookie::__construct() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
118
            null,
119
            $this->cookieDomain,
120
            $this->cookieSecure,
121
            $this->cookieHttpOnly
122
        ));
123
    }
124
125
    public static function getSubscribedEvents()
126
    {
127
        return [
128
            // Must be loaded early
129
            KernelEvents::REQUEST  => [['loadValueFromCookie', 20]],
130
            KernelEvents::RESPONSE => [['storeValueInCookie']]
131
        ];
132
    }
133
}
134