RouteConditionHandlerTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 31
c 1
b 0
f 0
dl 0
loc 96
ccs 36
cts 36
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPort() 0 3 1
A setPort() 0 4 1
A setName() 0 4 1
A checkAndReturnSelf() 0 10 2
A setScheme() 0 4 1
A isExtraConditionMatch() 0 17 6
A getName() 0 3 1
A setHost() 0 4 1
A getHost() 0 3 1
A getScheme() 0 3 1
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 78
    public function getHost(): ?string
33
    {
34 78
        return $this->host;
35
    }
36
37 72
    public function getName(): ?string
38
    {
39 72
        return $this->name;
40
    }
41
42 75
    public function getPort(): ?int
43
    {
44 75
        return $this->port;
45
    }
46
47 84
    public function getScheme(): ?string
48
    {
49 84
        return $this->scheme;
50
    }
51
52 18
    public function setHost(string $host): RouteConditionHandlerInterface
53
    {
54 18
        $this->host = $host;
55 18
        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 12
    public function setPort(int $port): RouteConditionHandlerInterface
65
    {
66 12
        $this->port = $port;
67 12
        return $this->checkAndReturnSelf();
68
    }
69
70 15
    public function setScheme(string $scheme): RouteConditionHandlerInterface
71
    {
72 15
        $this->scheme = $scheme;
73 15
        return $this->checkAndReturnSelf();
74
    }
75
76 33
    private function checkAndReturnSelf(): RouteConditionHandlerInterface
77
    {
78 33
        if ($this instanceof RouteConditionHandlerInterface) {
79 30
            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 72
    protected function isExtraConditionMatch(Route $route, ServerRequestInterface $request): bool
90
    {
91
        // check for scheme condition
92 72
        $scheme = $route->getScheme();
93 72
        if ($scheme !== null && $scheme !== $request->getUri()->getScheme()) {
94 6
            return false;
95
        }
96
97
        // check for domain condition
98 66
        $host = $route->getHost();
99 66
        if ($host !== null && $host !== $request->getUri()->getHost()) {
100 6
            return false;
101
        }
102
103
        // check for port condition
104 63
        $port = $route->getPort();
105 63
        return !($port !== null && $port !== $request->getUri()->getPort());
106
    }
107
}
108