Completed
Push — master ( 8e829c...fc40f2 )
by Neomerx
01:53
created

Cookie::isAccessibleThroughHttpAndScripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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