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

Request   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 55
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isMethod() 0 4 1
A fromGlobals() 0 3 1
A path() 0 6 1
A __construct() 0 5 1
A get() 0 5 1
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