Passed
Push — main ( 3a9c9f...17fbbc )
by Chema
09:55 queued 07:39
created

Request::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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