Passed
Pull Request — main (#8)
by Jesús
02:40 queued 28s
created

Request::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Entities;
6
7
final class Request
8
{
9
    public const METHOD_CONNECT = 'CONNECT';
10
    public const METHOD_DELETE = 'DELETE';
11
    public const METHOD_GET = 'GET';
12
    public const METHOD_HEAD = 'HEAD';
13
    public const METHOD_OPTIONS = 'OPTIONS';
14
    public const METHOD_PATCH = 'PATCH';
15
    public const METHOD_POST = 'POST';
16
    public const METHOD_PUT = 'PUT';
17
    public const METHOD_TRACE = 'TRACE';
18
    public const ALL_METHODS = [
19
        self::METHOD_CONNECT,
20
        self::METHOD_DELETE,
21
        self::METHOD_GET,
22
        self::METHOD_HEAD,
23
        self::METHOD_OPTIONS,
24
        self::METHOD_PATCH,
25
        self::METHOD_POST,
26
        self::METHOD_PUT,
27
        self::METHOD_TRACE,
28
    ];
29
30 74
    private function __construct(
31
        private array $query,
32
        private array $request,
33
        private array $server,
34
    ) {
35 74
    }
36
37 74
    public static function fromGlobals(): self
38
    {
39 74
        return new self($_GET, $_POST, $_SERVER);
40
    }
41
42 73
    public function isMethod(string $method): bool
43
    {
44
        /** @psalm-suppress PossiblyUndefinedArrayOffset */
45 73
        return (string)$this->server['REQUEST_METHOD'] === $method;
46
    }
47
48 67
    public function path(): string
49
    {
50
        /** @psalm-suppress PossiblyUndefinedArrayOffset */
51 67
        return (string)parse_url(
52 67
            (string)$this->server['REQUEST_URI'],
53 67
            PHP_URL_PATH,
54 67
        );
55
    }
56
57 1
    public function get(string $key): mixed
58
    {
59 1
        return $this->request[$key]
60 1
            ?? $this->query[$key]
61 1
            ?? null;
62
    }
63
}
64