Endpoint   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 122
ccs 32
cts 32
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getUniqueRoutes() 0 18 2
A getRoutes() 0 3 1
A getVars() 0 9 2
A getAllowedMethods() 0 3 1
A getPath() 0 3 1
A withRoute() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\SwitchRoute;
6
7
use OutOfBoundsException;
8
9
/**
10
 * Endpoint for routing.
11
 * @internal
12
 */
13
final class Endpoint
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $path;
19
20
    /**
21
     * @var array[]
22
     */
23
    protected $routes = [];
24
25
    /**
26
     * @var array[]
27
     */
28
    protected $vars = [];
29
30
31
    /**
32
     * Endpoint constructor.
33
     *
34
     * @param string $path
35
     */
36 18
    public function __construct(string $path)
37
    {
38 18
        $this->path = $path;
39
    }
40
41
    /**
42
     * Add a route for this endpoint.
43
     *
44
     * @param string $method
45
     * @param mixed  $route
46
     * @param array  $vars
47
     * @return static
48
     */
49 14
    public function withRoute(string $method, $route, array $vars): self
50
    {
51 14
        $method = strtoupper($method);
52
53 14
        if (isset($this->routes[$method])) {
54 3
            throw new InvalidRouteException("Duplicate route for '$method {$this->path}'");
55
        }
56
57 14
        $copy = clone $this;
58 14
        $copy->routes[$method] = $route;
59 14
        $copy->vars[$method] = $vars;
60
61 14
        return $copy;
62
    }
63
64
    /**
65
     * Get the path of this endpoint.
66
     *
67
     * @return string
68
     */
69 2
    public function getPath(): string
70
    {
71 2
        return $this->path;
72
    }
73
74
    /**
75
     * Get the allowed methods for this endpoint.
76
     *
77
     * @return array
78
     */
79 7
    public function getAllowedMethods(): array
80
    {
81 7
        return array_diff(array_keys($this->routes), ['']);
82
    }
83
84
    /**
85
     * Get all routes for this endpoint.
86
     *
87
     * @return array[]
88
     */
89 4
    public function getRoutes(): array
90
    {
91 4
        return $this->routes;
92
    }
93
94
    /**
95
     * Get vars for a route.
96
     *
97
     * @param string $method
98
     * @return array[]
99
     */
100 7
    public function getVars(string $method): array
101
    {
102 7
        $method = strtoupper($method);
103
104 7
        if (!isset($this->vars[$method])) {
105 1
            throw new OutOfBoundsException("Method '$method' not available for endpoint '{$this->path}'");
106
        }
107
108 6
        return $this->vars[$method];
109
    }
110
111
112
    /**
113
     * Get unique routes with methods and vars.
114
     *
115
     * @return \Generator
116
     */
117 4
    public function getUniqueRoutes(): \Generator
118
    {
119 4
        $queue = array_keys($this->routes);
120
121 4
        while ($queue !== []) {
122 1
            $method = reset($queue);
123
124 1
            $route = $this->routes[$method];
125 1
            $vars = $this->vars[$method];
126
127 1
            $methods = array_values(array_intersect(
128 1
                array_keys($this->routes, $route, true),
129 1
                array_keys($this->vars, $vars, true)
130
            ));
131
132 1
            yield [$methods, $route, $vars];
133
134 1
            $queue = array_diff($queue, $methods);
135
        }
136
    }
137
}
138