1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
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 Surfnet\StepupBundle\Service; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
22
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
|
25
|
|
|
final class LocaleCookieService |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $domain; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $secure; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $httpOnly; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
* @param string $domain |
50
|
|
|
* @param bool $secure |
51
|
|
|
* @param bool $httpOnly |
52
|
|
|
*/ |
53
|
|
|
public function __construct($name, $domain, $secure, $httpOnly) |
54
|
|
|
{ |
55
|
|
|
if (!is_string($name)) { |
56
|
|
|
throw InvalidArgumentException::invalidType('string', 'name', $name); |
57
|
|
|
} |
58
|
|
|
if (empty($name)) { |
59
|
|
|
throw new InvalidArgumentException('Empty name provided to ' . __CLASS__); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->name = $name; |
63
|
|
|
|
64
|
|
|
if (!is_string($domain)) { |
65
|
|
|
throw InvalidArgumentException::invalidType('string', 'domain', $domain); |
66
|
|
|
} |
67
|
|
|
if (empty($domain)) { |
68
|
|
|
throw new InvalidArgumentException(sprintf('Empty domain provided to ' . __CLASS__)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->domain = $domain; |
72
|
|
|
|
73
|
|
|
if (!is_bool($secure)) { |
74
|
|
|
throw InvalidArgumentException::invalidType('bool', 'secure', $secure); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->secure = $secure; |
78
|
|
|
|
79
|
|
|
if (!is_bool($httpOnly)) { |
80
|
|
|
throw InvalidArgumentException::invalidType('bool', 'httpOnly', $httpOnly); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->httpOnly = $httpOnly; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the given locale as a cookie on the given HTTP Response. |
88
|
|
|
* |
89
|
|
|
* @param string $locale |
90
|
|
|
* Locale to set as cookie value. |
91
|
|
|
* @param Response $response |
92
|
|
|
* HTTP response to add the cookie to. |
93
|
|
|
* |
94
|
|
|
* @return Response |
95
|
|
|
* Augmented Response. |
96
|
|
|
*/ |
97
|
|
|
public function set($locale, Response $response) |
98
|
|
|
{ |
99
|
|
|
$response->headers->setCookie(new Cookie($this->name, $locale, 0, '/', $this->domain, $this->secure, $this->httpOnly)); |
|
|
|
|
100
|
|
|
|
101
|
|
|
return $response; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.