Cookie::isAccessibleOnlyThroughHttp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Cookies;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeInterface;
22
use Limoncello\Application\Exceptions\InvalidArgumentException;
23
use Limoncello\Contracts\Cookies\CookieInterface;
24
25
/**
26
 * @package Limoncello\Application
27
 */
28
class Cookie implements CookieInterface
29
{
30
    /**
31
     * @var string
32
     */
33
    private $name;
34
35
    /**
36
     * @var string
37
     */
38
    private $value;
39
40
    /**
41
     * @var int
42
     */
43
    private $expire;
44
45
    /**
46
     * @var string
47
     */
48
    private $path;
49
50
    /**
51
     * @var string
52
     */
53
    private $domain;
54
55
    /**
56
     * @var bool
57
     */
58
    private $isSecure;
59
60
    /**
61
     * @var bool
62
     */
63
    private $isHttpOnly;
64
65
    /**
66
     * @var bool
67
     */
68
    private $isRaw;
69
70
    /**
71
     * @param string $name
72
     * @param string $value
73
     * @param int    $expire
74
     * @param string $path
75
     * @param string $domain
76
     * @param bool   $isSecure
77
     * @param bool   $isHttpOnly
78 3
     * @param bool   $isRaw
79
     */
80
    public function __construct(
81
        string $name,
82
        string $value,
83
        int $expire,
84
        string $path,
85
        string $domain,
86
        bool $isSecure,
87
        bool $isHttpOnly,
88 3
        bool $isRaw
89
    ) {
90 3
        $this->name = $name;
91 3
        $this
92 3
            ->setValue($value)
93 3
            ->setExpiresAtUnixTime($expire)
94
            ->setPath($path)
95 3
            ->setDomain($domain);
96 3
97 3
        $isSecure === true ? $this->setSendOnlyOverSecureConnection() : $this->setSendOverAnyConnection();
98
        $isHttpOnly === true ? $this->setAccessibleOnlyThroughHttp() : $this->setAccessibleThroughHttpAndScripts();
99
        $isRaw === true ? $this->setAsRaw() : $this->setAsNotRaw();
100
    }
101
102
    /**
103 3
     * @inheritdoc
104
     */
105 3
    public function getName(): string
106
    {
107
        return $this->name;
108
    }
109
110
    /**
111 3
     * @inheritdoc
112
     */
113 3
    public function getValue(): string
114
    {
115
        return $this->value;
116
    }
117
118
    /**
119 3
     * @inheritdoc
120
     */
121 3
    public function setValue(string $value): CookieInterface
122
    {
123 3
        $this->value = $value;
124
125
        return $this;
126
    }
127
128
    /**
129 3
     * @inheritdoc
130
     */
131 3
    public function getExpiresAtUnixTime(): int
132
    {
133
        return $this->expire;
134
    }
135
136
    /**
137 3
     * @inheritdoc
138
     */
139 3
    public function setExpiresAtUnixTime(int $unixTimestamp): CookieInterface
140 1
    {
141
        if ($unixTimestamp < 0) {
142
            throw new InvalidArgumentException('Timestamp cannot be negative.');
143 3
        }
144
145 3
        $this->expire = $unixTimestamp;
146
147
        return $this;
148
    }
149
150
    /**
151 1
     * @inheritdoc
152
     */
153 1
    public function setExpiresInSeconds(int $seconds): CookieInterface
154
    {
155
        return $this->setExpiresAtUnixTime(time() + (int)max(0, $seconds));
156
    }
157
158
    /**
159 1
     * @inheritdoc
160
     */
161 1
    public function setExpiresAtDataTime(DateTimeInterface $dateTime): CookieInterface
162
    {
163
        return $this->setExpiresAtUnixTime($dateTime->getTimestamp());
164
    }
165
166
    /**
167 3
     * @inheritdoc
168
     */
169 3
    public function getPath(): string
170
    {
171
        return $this->path;
172
    }
173
174
    /**
175 3
     * @inheritdoc
176
     */
177 3
    public function setPath(string $path): CookieInterface
178
    {
179 3
        $this->path = $path;
180
181
        return $this;
182
    }
183
184
    /**
185 3
     * @inheritdoc
186
     */
187 3
    public function getDomain(): string
188
    {
189
        return $this->domain;
190
    }
191
192
    /**
193 3
     * @inheritdoc
194
     */
195 3
    public function setDomain(string $domain): CookieInterface
196
    {
197 3
        $this->domain = $domain;
198
199
        return $this;
200
    }
201
202
    /**
203 3
     * @inheritdoc
204
     */
205 3
    public function isSendOnlyOverSecureConnection(): bool
206
    {
207
        return $this->isSecure;
208
    }
209
210
    /**
211 2
     * @inheritdoc
212
     */
213 2
    public function setSendOnlyOverSecureConnection(): CookieInterface
214
    {
215 2
        $this->isSecure = true;
216
217
        return $this;
218
    }
219
220
    /**
221 2
     * @inheritdoc
222
     */
223 2
    public function isSendOverAnyConnection(): bool
224
    {
225
        return !$this->isSecure;
226
    }
227
228
    /**
229 2
     * @inheritdoc
230
     */
231 2
    public function setSendOverAnyConnection(): CookieInterface
232
    {
233 2
        $this->isSecure = false;
234
235
        return $this;
236
    }
237
238
    /**
239 3
     * @inheritdoc
240
     */
241 3
    public function isAccessibleOnlyThroughHttp(): bool
242
    {
243
        return $this->isHttpOnly;
244
    }
245
246
    /**
247 2
     * @inheritdoc
248
     */
249 2
    public function setAccessibleOnlyThroughHttp(): CookieInterface
250
    {
251 2
        $this->isHttpOnly = true;
252
253
        return $this;
254
    }
255
256
    /**
257 2
     * @inheritdoc
258
     */
259 2
    public function isAccessibleThroughHttpAndScripts(): bool
260
    {
261
        return !$this->isHttpOnly;
262
    }
263
264
    /**
265 2
     * @inheritdoc
266
     */
267 2
    public function setAccessibleThroughHttpAndScripts(): CookieInterface
268
    {
269 2
        $this->isHttpOnly = false;
270
271
        return $this;
272
    }
273
274
    /**
275 2
     * @inheritdoc
276
     */
277 2
    public function isRaw(): bool
278
    {
279
        return $this->isRaw;
280
    }
281
282
    /**
283 3
     * @inheritdoc
284
     */
285 3
    public function setAsRaw(): CookieInterface
286
    {
287 3
        $this->isRaw = true;
288
289
        return $this;
290
    }
291
292
    /**
293 3
     * @inheritdoc
294
     */
295 3
    public function isNotRaw(): bool
296
    {
297
        return !$this->isRaw;
298
    }
299
300
    /**
301 2
     * @inheritdoc
302
     */
303 2
    public function setAsNotRaw(): CookieInterface
304
    {
305 2
        $this->isRaw = false;
306
307
        return $this;
308
    }
309
}
310