Input::parseRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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