Test Failed
Push — master ( 70a787...b2d464 )
by Erandir
03:12
created

Request::exceptSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PlugHttp;
4
5
use PlugHttp\Body\Content;
6
use PlugHttp\Globals\Cookie;
7
use PlugHttp\Globals\Env;
8
use PlugHttp\Globals\File;
9
use PlugHttp\Globals\Get;
10
use PlugHttp\Globals\Server;
11
use PlugHttp\Globals\Session;
12
13
/**
14
 * Class Request
15
 * @package PlugHttp
16
 */
17
class Request
18
{
19
    /**
20
     * @var Cookie
21
     */
22
    private Cookie $cookie;
23
24
    /**
25
     * @var Env
26
     */
27
    private Env $env;
28
29
    /**
30
     * @var File
31
     */
32
    private File $file;
33
34
    /**
35
     * @var Get
36
     */
37
    private Get $get;
38
39
    /**
40
     * @var Server
41
     */
42
    private Server $server;
43
44
    /**
45
     * @var Session
46
     */
47
    private Session $session;
48
49
    /**
50
     * @var Content
51
     */
52
    private Content $content;
53
54
    /**
55
     * Request constructor.
56
     */
57
    public function __construct()
58
    {
59
        $this->cookie = new Cookie();
60
        $this->env = new Env();
61
        $this->file = new File();
62
        $this->get = new Get();
63
        $this->server = new Server();
64
        $this->session = new Session();
65
        $this->content = new Content($this->server);
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param $value
71
     */
72
    public function addCookie(string $key, $value, $expire = 0, $path = "", $domain = "", $secure = false, $httponly = false): void
73
    {
74
        $this->cookie->add($key, $value, $expire, $path, $domain, $secure, $httponly);
75
    }
76
77
    /**
78
     * @param string|null $key
79
     * @return array|mixed
80
     */
81
    public function cookies(?string $key = null)
82
    {
83
        if ($key) {
84
            return $this->cookie->get($key);
85
        }
86
87
        return $this->cookie->all();
88
    }
89
90
    /**
91
     * @param $key
92
     * @return bool
93
     */
94
    public function hasCookie($key): bool
95
    {
96
        return $this->cookie->has($key);
97
    }
98
99
    /**
100
     * @param $key
101
     */
102
    public function removeCookie($key): void
103
    {
104
        $this->cookie->remove($key);
105
    }
106
107
    /**
108
     * @param array $values
109
     * @return array
110
     */
111
    public function exceptCookie(array $values): array
112
    {
113
        return $this->cookie->except($values);
114
    }
115
116
    /**
117
     * @param array $values
118
     * @return array
119
     */
120
    public function onlyCookie(array $values): array
121
    {
122
        return $this->cookie->only($values);
123
    }
124
125
    /**
126
     * @param string|null $key
127
     * @return array|mixed
128
     */
129
    public function query(?string $key = null)
130
    {
131
        if ($key) {
132
            return $this->get->get($key);
133
        }
134
135
        return $this->get->all();
136
    }
137
138
    /**
139
     * @param string $key
140
     * @param $value
141
     */
142
    public function addQuery(string $key, $value): void
143
    {
144
        $this->get->add($key, $value);
145
    }
146
147
    /**
148
     * @param array $values
149
     * @return array
150
     */
151
    public function onlyQuery(array $values): array
152
    {
153
        return $this->get->only($values);
154
    }
155
156
    /**
157
     * @param array $values
158
     * @return array
159
     */
160
    public function exceptQuery(array $values): array
161
    {
162
        return $this->get->except($values);
163
    }
164
165
    /**
166
     * @param string $value
167
     * @return bool
168
     */
169
    public function hasQuery(string $value): bool
170
    {
171
        return $this->get->has($value);
172
    }
173
174
    /**
175
     * @param string $key
176
     */
177
    public function removeQuery(string $key): void
178
    {
179
        $this->get->remove($key);
180
    }
181
182
    /**
183
     * @param string|null $key
184
     * @return array|mixed
185
     */
186
    public function env(?string $key = null)
187
    {
188
        if ($key) {
189
            return $this->env->get($key);
190
        }
191
192
        return $this->env->all();
193
    }
194
195
    /**
196
     * @param string $key
197
     * @param $value
198
     */
199
    public function addEnv(string $key, $value): void
200
    {
201
        $this->env->add($key, $value);
202
    }
203
204
    /**
205
     * @param array $values
206
     * @return array
207
     */
208
    public function onlyEnv(array $values): array
209
    {
210
        return $this->env->only($values);
211
    }
212
213
    /**
214
     * @param array $values
215
     * @return array
216
     */
217
    public function exceptEnv(array $values): array
218
    {
219
        return $this->env->except($values);
220
    }
221
222
    /**
223
     * @param string $value
224
     * @return bool
225
     */
226
    public function hasEnv(string $value): bool
227
    {
228
        return $this->env->has($value);
229
    }
230
231
    /**
232
     * @param string $key
233
     */
234
    public function removeEnv(string $key): void
235
    {
236
        $this->env->remove($key);
237
    }
238
239
    /**
240
     * @param string|null $key
241
     * @return array
242
     */
243
    public function file(?string $key = null)
244
    {
245
        if ($key) {
246
            return $this->file->get($key);
247
        }
248
249
        return $this->file->all();
250
    }
251
252
    /**
253
     * @param $key
254
     * @return bool
255
     */
256
    public function hasFile($key): bool
257
    {
258
        return $this->file->has($key);
259
    }
260
261
    /**
262
     * @param $key
263
     */
264
    public function removeFile($key): void
265
    {
266
        $this->file->remove($key);
267
    }
268
269
    /**
270
     * @param array $values
271
     * @return array
272
     */
273
    public function exceptFile(array $values): array
274
    {
275
        return $this->file->except($values);
276
    }
277
278
    /**
279
     * @param array $values
280
     * @return array
281
     */
282
    public function onlyFile(array $values): array
283
    {
284
        return $this->file->only($values);
285
    }
286
287
    /**
288
     * @param string $key
289
     * @param $value
290
     */
291
    public function addHeader(string $key, $value): void
292
    {
293
        $this->server->add($key, $value);
294
    }
295
296
    /**
297
     * @param string $key
298
     * @return array|mixed
299
     */
300
    public function header(string $key)
301
    {
302
        return $this->server->get($key);
303
    }
304
305
    /**
306
     * @param $key
307
     * @return bool
308
     */
309
    public function hasheader($key): bool
310
    {
311
        return $this->server->has($key);
312
    }
313
314
    /**
315
     * @param $key
316
     */
317
    public function removeHeader($key): void
318
    {
319
        $this->server->remove($key);
320
    }
321
322
    /**
323
     * @param array $values
324
     * @return array
325
     */
326
    public function exceptHeaders(array $values): array
327
    {
328
        return $this->server->except($values);
329
    }
330
331
    /**
332
     * @param array $values
333
     * @return array
334
     */
335
    public function onlyHeaders(array $values): array
336
    {
337
        return $this->server->only($values);
338
    }
339
340
    /**
341
     * @return string
342
     */
343
    public function method(): string
344
    {
345
        return $this->server->method();
346
    }
347
348
    /**
349
     * @return string
350
     */
351
    public function getUrl(): string
352
    {
353
        return $this->server->getUrl();
354
    }
355
356
    /**
357
     * @param string $method
358
     * @return bool
359
     */
360
    public function isMethod(string $method): bool
361
    {
362
        return $this->method() === strtoupper($method);
363
    }
364
365
    /**
366
     * @return array
367
     */
368
    public function headers(): array
369
    {
370
        return $this->server->all();
371
    }
372
373
    /**
374
     * @param string $path
375
     * @param int $code
376
     * @return bool
377
     */
378
    public function redirect(string $path, int $code = 301)
379
    {
380
        header("HTTP/1.0 {$code}");
381
        header("Location: {$path}");
382
383
        return true;
384
    }
385
386
    /**
387
     * @param string $key
388
     * @param $value
389
     */
390
    public function addSession(string $key, $value): void
391
    {
392
        $this->session->add($key, $value);
393
    }
394
395
    /**
396
     * @param string|null $key
397
     * @return array|mixed
398
     */
399
    public function session(?string $key = null)
400
    {
401
        if ($key) {
402
            return $this->session->get($key);
403
        }
404
405
        return $this->session->all();
406
    }
407
408
    /**
409
     * @param $key
410
     * @return bool
411
     */
412
    public function hasSession($key): bool
413
    {
414
        return $this->session->has($key);
415
    }
416
417
    /**
418
     * @param $key
419
     */
420
    public function removeSession($key): void
421
    {
422
        $this->session->remove($key);
423
    }
424
425
    /**
426
     * @param array $values
427
     * @return array
428
     */
429
    public function exceptSession(array $values): array
430
    {
431
        return $this->session->except($values);
432
    }
433
434
    /**
435
     * @param array $values
436
     * @return array
437
     */
438
    public function onlySession(array $values): array
439
    {
440
        return $this->session->only($values);
441
    }
442
443
    /**
444
     * @param array $values
445
     * @return array
446
     */
447
    public function except(array $values)
448
    {
449
        return $this->content->except($values);
450
    }
451
452
    /**
453
     * @param array $values
454
     * @return array
455
     */
456
    public function only(array $values)
457
    {
458
        return $this->content->only($values);
459
    }
460
461
    /**
462
     * @param string $key
463
     * @return bool
464
     */
465
    public function has(string $key): bool
466
    {
467
        return $this->content->has($key);
468
    }
469
470
    /**
471
     * @param string $key
472
     */
473
    public function remove(string $key): void
474
    {
475
        $this->content->remove($key);
476
    }
477
478
    /**
479
     * @param $key
480
     * @param $value
481
     */
482
    public function add($key, $value)
483
    {
484
        $this->content->add($key, $value);
485
    }
486
487
    /**
488
     * @return array
489
     */
490
    public function all(): array
491
    {
492
        return $this->content->all();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->content->all() could return the type string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
493
    }
494
495
    /**
496
     * @param string $value
497
     * @return mixed
498
     */
499
    public function get(string $value)
500
    {
501
        return $this->content->get($value);
502
    }
503
504
    /**
505
     * @param $name
506
     */
507
    public function __get($name)
508
    {
509
        return $this->content->$name;
510
    }
511
}