WebRouter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 2 Features 1
Metric Value
eloc 25
c 14
b 2
f 1
dl 0
loc 74
ccs 10
cts 10
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuery() 0 33 6
A generate() 0 3 1
A __construct() 0 4 1
A match() 0 8 1
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
12
use function file_get_contents;
13
use function json_decode;
14
use function json_last_error;
15
use function json_last_error_msg;
16
use function parse_str;
17
use function parse_url;
18
use function rtrim;
19
use function str_contains;
20
use function strtolower;
21 5
22
use const JSON_ERROR_NONE;
23 5
use const PHP_URL_PATH;
24 5
25
/**
26
 * @psalm-import-type Globals from RouterInterface
27
 * @psalm-import-type Server from RouterInterface
28
 */
29 2
final class WebRouter implements RouterInterface
30
{
31 2
    public function __construct(
32 2
        #[DefaultSchemeHost]
33 2
        private string $schemeHost,
34 2
    ) {
35 2
    }
36 2
37
    /**
38
     * {@inheritDoc}
39 2
     *
40
     * @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...
41
     * @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...
42
     */
43
    public function match(array $globals, array $server)
44
    {
45 1
        $method = strtolower($server['REQUEST_METHOD']);
46
47 1
        return new RouterMatch(
48
            $method,
49
            $this->schemeHost . parse_url($server['REQUEST_URI'], PHP_URL_PATH),
50
            $this->getQuery($method, $globals, $server),
51
        );
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function generate($name, $data)
58
    {
59
        return false;
60
    }
61
62
    /**
63
     * Return request query by media-type
64
     *
65
     * @param Server  $server
66
     * @param Globals $globals
67
     *
68
     * @return array<string, mixed>
69
     */
70
    private function getQuery(string $method, array $globals, array $server): array
71
    {
72
        if ($method === 'get') {
73
            return $globals['_GET'];
74
        }
75
76
        if ($method === 'post') {
77
            return $globals['_POST'];
78
        }
79
80
        $contentType = $server['CONTENT_TYPE'] ?? $server['HTTP_CONTENT_TYPE'] ?? '';
81
        $isFormUrlEncoded = str_contains($contentType, 'application/x-www-form-urlencoded');
82
        $rawBody = $server['HTTP_RAW_POST_DATA'] ?? rtrim((string) file_get_contents('php://input'));
83
        if ($isFormUrlEncoded) {
84
            parse_str(rtrim($rawBody), $put);
85
86
            /** @var array<string, mixed> $put */
87
            return $put;
88
        }
89
90
        $isApplicationJson = str_contains($contentType, 'application/json');
91
        if (! $isApplicationJson) {
92
            return [];
93
        }
94
95
        /** @var array<string, mixed> $content */
96
        $content = json_decode($rawBody, true);
97
        $error = json_last_error();
98
        if ($error !== JSON_ERROR_NONE) {
99
            throw new BadRequestJsonException(json_last_error_msg());
100
        }
101
102
        return $content;
103
    }
104
}
105