Passed
Push — main ( fe77ac...556dc2 )
by Chema
02:14
created

Request::isMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router;
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
19 42
    public function __construct(
20
        private array $query,
21
        private array $request,
22
        private array $server,
23
    ) {
24 42
    }
25
26 42
    public static function fromGlobals(): self
27
    {
28 42
        return new self($_GET, $_POST, $_SERVER);
29
    }
30
31 42
    public function isMethod(string $method): bool
32
    {
33
        /** @psalm-suppress PossiblyUndefinedArrayOffset */
34 42
        return (string)$this->server['REQUEST_METHOD'] === $method;
35
    }
36
37 41
    public function path(): string
38
    {
39
        /** @psalm-suppress PossiblyUndefinedArrayOffset */
40 41
        return (string)parse_url(
41 41
            (string)$this->server['REQUEST_URI'],
42 41
            PHP_URL_PATH,
43 41
        );
44
    }
45
46
    public function get(string $key): mixed
47
    {
48
        return $this->request[$key]
49
            ?? $this->query[$key]
50
            ?? null;
51
    }
52
}
53