WebRouter::getQuery()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
nc 6
nop 3
dl 0
loc 33
ccs 0
cts 0
cp 0
crap 42
rs 9.0444
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Sunday\Provide\Router;
6
7
use BEAR\Sunday\Annotation\DefaultSchemeHost;
8
use BEAR\Sunday\Exception\BadRequestJsonException;
9
use BEAR\Sunday\Extension\Router\RouterInterface;
10
use BEAR\Sunday\Extension\Router\RouterMatch;
11
use Override;
12
13
use function file_get_contents;
14
use function json_decode;
15
use function json_last_error;
16
use function json_last_error_msg;
17
use function parse_str;
18
use function parse_url;
19
use function rtrim;
20
use function str_contains;
21 5
use function strtolower;
22
23 5
use const JSON_ERROR_NONE;
24 5
use const PHP_URL_PATH;
25
26
/**
27
 * @psalm-import-type Globals from RouterInterface
28
 * @psalm-import-type Server from RouterInterface
29 2
 */
30
final class WebRouter implements RouterInterface
31 2
{
32 2
    private string $schemeHost;
33 2
34 2
    public function __construct(
35 2
        #[DefaultSchemeHost]
36 2
        string $schemeHost,
37
    ) {
38
        $this->schemeHost = rtrim($schemeHost, '/');
39 2
    }
40
41
    /**
42
     * {@inheritDoc}
43
     *
44
     * @param Globals $globals
0 ignored issues
show
Bug introduced by
The type BEAR\Sunday\Provide\Router\Globals was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45 1
     * @param Server  $server
0 ignored issues
show
Bug introduced by
The type BEAR\Sunday\Provide\Router\Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
     */
47 1
    #[Override]
48
    public function match(array $globals, array $server)
49
    {
50
        $method = strtolower($server['REQUEST_METHOD']);
51
        $path = parse_url($server['REQUEST_URI'], PHP_URL_PATH);
52
        $path = $path === false || $path === null ? '/' : $path;
53
54
        return new RouterMatch(
55
            $method,
56
            $this->schemeHost . $path,
57
            $this->getQuery($method, $globals, $server),
58
        );
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    #[Override]
65
    public function generate($name, $data)
66
    {
67
        return false;
68
    }
69
70
    /**
71
     * Return request query by media-type
72
     *
73
     * @param Server  $server
74
     * @param Globals $globals
75
     *
76
     * @return array<string, mixed>
77
     * @psalm-return array<string, mixed>
78
     */
79
    private function getQuery(string $method, array $globals, array $server): array
80
    {
81
        if ($method === 'get') {
82
            return $globals['_GET'];
83
        }
84
85
        if ($method === 'post') {
86
            return $globals['_POST'];
87
        }
88
89
        $contentType = $server['CONTENT_TYPE'] ?? $server['HTTP_CONTENT_TYPE'] ?? '';
90
        $isFormUrlEncoded = str_contains($contentType, 'application/x-www-form-urlencoded');
91
        $rawBody = $server['HTTP_RAW_POST_DATA'] ?? rtrim((string) file_get_contents('php://input'));
92
        if ($isFormUrlEncoded) {
93
            parse_str(rtrim($rawBody), $put);
94
95
            /** @var array<string, mixed> $put @phpstan-ignore varTag.nativeType */
96
            return $put;
97
        }
98
99
        $isApplicationJson = str_contains($contentType, 'application/json');
100
        if (! $isApplicationJson) {
101
            return [];
102
        }
103
104
        /** @var array<string, mixed> $content */
105
        $content = json_decode($rawBody, true);
106
        $error = json_last_error();
107
        if ($error !== JSON_ERROR_NONE) {
108
            throw new BadRequestJsonException(json_last_error_msg());
109
        }
110
111
        return $content;
112
    }
113
}
114