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

Request   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 53.85%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 43
rs 10
c 0
b 0
f 0
ccs 7
cts 13
cp 0.5385
wmc 5

5 Methods

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