Completed
Push — master ( 32e652...8e829c )
by Neomerx
11:56
created

Cookie::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.0534
cc 4
eloc 18
nc 8
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
    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
        $this->name = $name;
89
        $this
90
            ->setValue($value)
91
            ->setExpiresAtUnixTime($expire)
92
            ->setPath($path)
93
            ->setDomain($domain);
94
95
        $isSecure === true ? $this->setSendOnlyOverSecureConnection() : $this->setSendOverAnyConnection();
96
        $isHttpOnly === true ? $this->setAccessibleOnlyThroughHttp() : $this->setAccessibleThroughHttpAndScripts();
97
        $isRaw === true ? $this->setAsRaw() : $this->setAsNotRaw();
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function getName(): string
104
    {
105
        return $this->name;
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function getValue(): string
112
    {
113
        return $this->value;
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function setValue(string $value): CookieInterface
120
    {
121
        $this->value = $value;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function getExpiresAtUnixTime(): int
130
    {
131
        return $this->expire;
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function setExpiresAtUnixTime(int $unixTimestamp): CookieInterface
138
    {
139
        if ($unixTimestamp < 0) {
140
            throw new InvalidArgumentException($unixTimestamp);
141
        }
142
143
        $this->expire = $unixTimestamp;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function setExpiresInSeconds(int $seconds): CookieInterface
152
    {
153
        return $this->setExpiresAtUnixTime(time() + (int)max(0, $seconds));
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function setExpiresAtDataTime(DateTimeInterface $dateTime): CookieInterface
160
    {
161
        return $this->setExpiresAtUnixTime($dateTime->getTimestamp());
162
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167
    public function getPath(): string
168
    {
169
        return $this->path;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    public function setPath(string $path): CookieInterface
176
    {
177
        $this->path = $path;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185
    public function getDomain(): string
186
    {
187
        return $this->domain;
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193
    public function setDomain(string $domain): CookieInterface
194
    {
195
        $this->domain = $domain;
196
197
        return $this;
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203
    public function getSendOnlyOverSecureConnection(): bool
204
    {
205
        return $this->isSecure;
206
    }
207
208
    /**
209
     * @inheritdoc
210
     */
211
    public function setSendOnlyOverSecureConnection(): CookieInterface
212
    {
213
        $this->isSecure = true;
214
215
        return $this;
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function getSendOverAnyConnection(): bool
222
    {
223
        return !$this->isSecure;
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229
    public function setSendOverAnyConnection(): CookieInterface
230
    {
231
        $this->isSecure = false;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239
    public function getAccessibleOnlyThroughHttp(): bool
240
    {
241
        return $this->isHttpOnly;
242
    }
243
244
    /**
245
     * @inheritdoc
246
     */
247
    public function setAccessibleOnlyThroughHttp(): CookieInterface
248
    {
249
        $this->isHttpOnly = true;
250
251
        return $this;
252
    }
253
254
    /**
255
     * @inheritdoc
256
     */
257
    public function getAccessibleThroughHttpAndScripts(): bool
258
    {
259
        return !$this->isHttpOnly;
260
    }
261
262
    /**
263
     * @inheritdoc
264
     */
265
    public function setAccessibleThroughHttpAndScripts(): CookieInterface
266
    {
267
        $this->isHttpOnly = false;
268
269
        return $this;
270
    }
271
272
    /**
273
     * @inheritdoc
274
     */
275
    public function getIsRaw(): bool
276
    {
277
        return $this->isRaw;
278
    }
279
280
    /**
281
     * @inheritdoc
282
     */
283
    public function setAsRaw(): CookieInterface
284
    {
285
        $this->isRaw = true;
286
287
        return $this;
288
    }
289
290
    /**
291
     * @inheritdoc
292
     */
293
    public function getIsNotRaw(): bool
294
    {
295
        return !$this->isRaw;
296
    }
297
298
    /**
299
     * @inheritdoc
300
     */
301
    public function setAsNotRaw(): CookieInterface
302
    {
303
        $this->isRaw = false;
304
305
        return $this;
306
    }
307
}
308