Input   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 61
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 12 4
A getAuthorization() 0 4 1
A parseRequest() 0 5 1
A getParameter() 0 4 1
A __construct() 0 4 1
1
<?php
2
3
namespace kalanis\OAuth2\Http;
4
5
6
use Nette\Http\IRequest;
7
8
9
/**
10
 * Input parser
11
 * @package kalanis\OAuth2\Http
12
 */
13 1
class Input implements IInput
14
{
15
16
    /** @var array<string|int, mixed>|null */
17
    private ?array $data = null;
18
19 1
    public function __construct(
20
        private readonly IRequest $request,
21
    )
22
    {
23 1
    }
24
25
    /**
26
     * Get single parameter by key
27
     * @param string $name
28
     * @return mixed
29
     */
30
    public function getParameter(string $name): mixed
31
    {
32 1
        $parameters = $this->getParameters();
33 1
        return $parameters[$name] ?? null;
34
    }
35
36
    /**
37
     * Get all parameters
38
     * @return array<string|int, mixed>
39
     */
40
    public function getParameters(): array
41
    {
42 1
        if (is_null($this->data)) {
43 1
            if ($this->request->getQuery()) {
44
                $this->data = $this->request->getQuery();
45 1
            } else if ($this->request->getPost()) {
46 1
                $this->data = $this->request->getPost();
47
            } else {
48
                $this->data = $this->parseRequest(strval(file_get_contents('php://input')));
49
            }
50
        }
51 1
        return $this->data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    /**
55
     * Convert client request data to array or traversable
56
     * @param string $data
57
     * @return array<string|int, mixed>
58
     */
59
    private function parseRequest(string $data): array
60
    {
61
        $result = [];
62
        parse_str($data, $result);
63
        return $result;
64
    }
65
66
    /**
67
     * Get authorization token from header - Authorization: Bearer
68
     * @return string|null
69
     */
70
    public function getAuthorization(): ?string
71
    {
72 1
        $authorization = explode(' ', (string) $this->request->getHeader('Authorization'));
73 1
        return $authorization[1] ?? null;
74
    }
75
}
76