Completed
Push — develop ( 511001...8b4d6d )
by A.
03:05
created

SingleCookieStorage::loadValueFromCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 OpenConext\Profile\Assert;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
use Symfony\Component\HttpFoundation\Cookie;
24
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
25
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
26
use Symfony\Component\HttpKernel\KernelEvents;
27
28
class SingleCookieStorage implements EventSubscriberInterface
29
{
30
    /**
31
     * @var string
32
     */
33
    private $cookieDomain;
34
35
    /**
36
     * @var string
37
     */
38
    private $cookieKey;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $cookieValue;
44
45
    public function __construct($cookieDomain, $cookieKey)
46
    {
47
        Assert::string($cookieDomain);
48
        Assert::string($cookieKey);
49
50
        $this->cookieDomain = $cookieDomain;
51
        $this->cookieKey    = $cookieKey;
52
    }
53
54
    /**
55
     * @param string $value
56
     */
57
    public function setValue($value)
58
    {
59
        Assert::string($value);
60
61
        $this->cookieValue = $value;
62
    }
63
64
    /**
65
     * @return string|null
66
     */
67
    public function getValue()
68
    {
69
        return $this->cookieValue;
70
    }
71
72
    /**
73
     * @param GetResponseEvent $event
74
     */
75
    public function loadValueFromCookie(GetResponseEvent $event)
76
    {
77
        $this->cookieValue = $event->getRequest()->cookies->get($this->cookieKey, null);
78
    }
79
80
    /**
81
     * @param FilterResponseEvent $event
82
     */
83
    public function storeValueInCookie(FilterResponseEvent $event)
84
    {
85
        $event->getResponse()->headers->setCookie(new Cookie(
86
            $this->cookieKey,
87
            $this->cookieValue,
88
            0,
89
            null,
90
            $this->cookieDomain,
91
            true
92
        ));
93
    }
94
95
    public static function getSubscribedEvents()
96
    {
97
        return [
98
            // Must be loaded early
99
            KernelEvents::REQUEST  => [['loadValueFromCookie', 20]],
100
            KernelEvents::RESPONSE => [['storeValueInCookie']]
101
        ];
102
    }
103
}
104