Passed
Push — 5.x ( 8bdbf0...b66c10 )
by Phil
01:48
created

isExtraConditionMatch()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2222
cc 6
nc 4
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use RuntimeException;
9
10
trait RouteConditionHandlerTrait
11
{
12
    /**
13
     * @var ?string
14
     */
15
    protected $host;
16
17
    /**
18
     * @var ?string
19
     */
20
    protected $name;
21
22
    /**
23
     * @var ?int
24
     */
25
    protected $port;
26
27
    /**
28
     * @var ?string
29
     */
30
    protected $scheme;
31
32 57
    public function getHost(): ?string
33
    {
34 57
        return $this->host;
35
    }
36
37 57
    public function getName(): ?string
38
    {
39 57
        return $this->name;
40
    }
41
42 54
    public function getPort(): ?int
43
    {
44 54
        return $this->port;
45
    }
46
47 60
    public function getScheme(): ?string
48
    {
49 60
        return $this->scheme;
50
    }
51
52 15
    public function setHost(string $host): RouteConditionHandlerInterface
53
    {
54 15
        $this->host = $host;
55 15
        return $this->checkAndReturnSelf();
56
    }
57
58 9
    public function setName(string $name): RouteConditionHandlerInterface
59
    {
60 9
        $this->name = $name;
61 9
        return $this->checkAndReturnSelf();
62
    }
63
64 9
    public function setPort(int $port): RouteConditionHandlerInterface
65
    {
66 9
        $this->port = $port;
67 9
        return $this->checkAndReturnSelf();
68
    }
69
70 9
    public function setScheme(string $scheme): RouteConditionHandlerInterface
71
    {
72 9
        $this->scheme = $scheme;
73 9
        return $this->checkAndReturnSelf();
74
    }
75
76 27
    private function checkAndReturnSelf(): RouteConditionHandlerInterface
77
    {
78 27
        if ($this instanceof RouteConditionHandlerInterface) {
79 24
            return $this;
80
        }
81
82 3
        throw new RuntimeException(sprintf(
83 3
            'Trait (%s) must be consumed by an instance of (%s)',
84 3
            __TRAIT__,
85 3
            RouteConditionHandlerInterface::class
86
        ));
87
    }
88
89 48
    protected function isExtraConditionMatch(Route $route, ServerRequestInterface $request): bool
90
    {
91
        // check for scheme condition
92 48
        $scheme = $route->getScheme();
93 48
        if ($scheme !== null && $scheme !== $request->getUri()->getScheme()) {
94 3
            return false;
95
        }
96
97
        // check for domain condition
98 45
        $host = $route->getHost();
99 45
        if ($host !== null && $host !== $request->getUri()->getHost()) {
100 6
            return false;
101
        }
102
103
        // check for port condition
104 42
        $port = $route->getPort();
105 42
        return !($port !== null && $port !== $request->getUri()->getPort());
106
    }
107
}
108