Passed
Push — master ( 8df7ad...310ad9 )
by Divine Niiquaye
07:56
created

ValidationTrait::compareDomain()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
nc 3
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
c 3
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Traits;
19
20
use Flight\Routing\Exceptions\MethodNotAllowedException;
21
use Flight\Routing\Exceptions\UriHandlerException;
22
use Flight\Routing\Interfaces\RouteInterface;
23
use Flight\Routing\Router;
24
use Psr\Http\Message\ServerRequestInterface;
25
26
trait ValidationTrait
27
{
28
    /**
29
     * Check if given request method matches given route method.
30
     *
31
     * @param string[] $routeMethod
32
     * @param string   $requestMethod
33
     *
34
     * @return bool
35
     */
36 46
    private function compareMethod(array $routeMethod, string $requestMethod): bool
37
    {
38 46
        return \in_array($requestMethod, $routeMethod, true);
39
    }
40
41
    /**
42
     * Check if given request domain matches given route domain.
43
     *
44
     * @param null|string              $routeDomain
45
     * @param string                   $requestDomain
46
     * @param array<int|string,string> $parameters
47
     *
48
     * @return bool
49
     */
50 47
    private function compareDomain(?string $routeDomain, string $requestDomain, array &$parameters): bool
51
    {
52 47
        return ($routeDomain === null || empty($routeDomain)) ||
53 47
            (bool) \preg_match($routeDomain, $requestDomain, $parameters);
54
    }
55
56
    /**
57
     * Check if given request uri matches given uri method.
58
     *
59
     * @param string                   $routeUri
60
     * @param string                   $requestUri
61
     * @param array<int|string,string> $parameters
62
     *
63
     * @return bool
64
     */
65 49
    private function compareUri(string $routeUri, string $requestUri, array &$parameters): bool
66
    {
67 49
        return (bool) \preg_match($routeUri, $requestUri, $parameters);
68
    }
69
70
    /**
71
     * Check if given request uri scheme matches given route scheme.
72
     *
73
     * @param string[] $routeScheme
74
     * @param string   $requestScheme
75
     *
76
     * @return bool
77
     */
78 44
    private function compareScheme(array $routeScheme, string $requestScheme): bool
79
    {
80 44
        return empty($routeScheme) || \in_array($requestScheme, $routeScheme, true);
81
    }
82
83
    /**
84
     * Asserts the Route's method and domain scheme.
85
     *
86
     * @param RouteInterface   $route
87
     * @param array<int,mixed> $attributes
88
     */
89 46
    private function assertRoute(RouteInterface $route, array $attributes): void
90
    {
91 46
        [$method, $scheme, $path] = $attributes;
92
93 46
        if (!$this->compareMethod($route->getMethods(), $method)) {
94 2
            throw new MethodNotAllowedException($route->getMethods(), $path, $method);
95
        }
96
97 44
        if (!$this->compareScheme($route->getSchemes(), $scheme)) {
98 1
            throw new UriHandlerException(
99 1
                \sprintf('Unfortunately current scheme "%s" is not allowed on requested uri [%s]', $scheme, $path),
100 1
                400
101
            );
102
        }
103 43
    }
104
105
    /**
106
     * Get merged default parameters.
107
     *
108
     * @param array<int|string,mixed> $params
109
     * @param array<string,string>    $defaults
110
     *
111
     * @return array<string,string> Merged default parameters
112
     */
113 43
    private function mergeDefaults(array $params, array $defaults): array
114
    {
115 43
        foreach ($params as $key => $value) {
116 43
            if (!\is_int($key) && (!isset($defaults[$key]) || null !== $value)) {
117 6
                $defaults[$key] = $value;
118
            }
119
        }
120
121 43
        return $defaults;
122
    }
123
124
    /**
125
     * Merge Router attributes in route default and patterns.
126
     *
127
     * @param RouteInterface $route
128
     *
129
     * @return RouteInterface
130
     */
131 49
    private function mergeAttributes(RouteInterface $route): RouteInterface
132
    {
133 49
        foreach ($this->attributes as $type => $attributes) {
134 1
            if (Router::TYPE_DEFAULT === $type) {
135 1
                $route->setDefaults($attributes);
136
137 1
                continue;
138
            }
139
140 1
            $route->setPatterns($attributes);
141
        }
142
143 49
        return $route;
144
    }
145
146
    /**
147
     * @param ServerRequestInterface $request
148
     * @param string                 $name
149
     */
150 3
    private function getResourceMethod(ServerRequestInterface $request, string $name): string
151
    {
152 3
        return \strtolower($request->getMethod()) . \ucfirst($name);
153
    }
154
}
155