Router::dispatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
crap 2.003
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Application;
4
5
use FastRoute\Dispatcher;
6
use FastRoute\Dispatcher\GroupCountBased;
7
use FastRoute\RouteCollector;
8
use GuzzleHttp\Psr7\Request;
9
use GuzzleHttp\Psr7\Response;
10
use Stitcher\App;
11
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
12
13
class Router
14
{
15
    /** @var \FastRoute\RouteCollector */
16
    protected $routeCollector;
17
18
    /** @var array */
19
    protected $redirects = [];
20
21 1
    public function __construct(RouteCollector $routeCollector)
22
    {
23 1
        $this->routeCollector = $routeCollector;
24 1
    }
25
26 1
    public function get(string $url, string $controller): Router
27
    {
28 1
        $this->routeCollector->addRoute('GET', $url, [$controller, 'handle']);
29
30 1
        return $this;
31
    }
32
33
    public function put(string $url, string $controller): Router
34
    {
35
        $this->routeCollector->addRoute('PUT', $url, [$controller, 'handle']);
36
37
        return $this;
38
    }
39
40
    public function post(string $url, string $controller): Router
41
    {
42
        $this->routeCollector->addRoute('POST', $url, [$controller, 'handle']);
43
44
        return $this;
45
    }
46
47
    public function patch(string $url, string $controller): Router
48
    {
49
        $this->routeCollector->addRoute('PATCH', $url, [$controller, 'handle']);
50
51
        return $this;
52
    }
53
54
    public function delete(string $url, string $controller): Router
55
    {
56
        $this->routeCollector->addRoute('DELETE', $url, [$controller, 'handle']);
57
58
        return $this;
59
    }
60
61
    public function redirect(string $url, string $targetUrl): Router
62
    {
63
        $this->redirects[$url] = $targetUrl;
64
65
        return $this;
66
    }
67
68
    public function getRedirectForUrl(string $url): ?string
69
    {
70
        return $this->redirects[$url] ?? null;
71
    }
72
73 1
    public function dispatch(Request $request): ?Response
74
    {
75 1
        $dispatcher = new GroupCountBased($this->routeCollector->getData());
76
77 1
        $routeInfo = $dispatcher->dispatch(
78 1
            $request->getMethod(),
79 1
            $request->getUri()->getPath()
80
        );
81
82 1
        if ($routeInfo[0] !== Dispatcher::FOUND) {
83
            return null;
84
        }
85
86 1
        $handler = $this->resolveHandler($routeInfo[1]);
87 1
        $parameters = $routeInfo[2];
88
89
        return \call_user_func_array(
90 1
            $handler,
91 1
            array_merge([$request], $parameters)
92
        );
93
    }
94
95 1
    protected function resolveHandler(array $callback): array
96
    {
97 1
        $className = $callback[0];
98
99
        try {
100 1
            $handler = App::get($className);
101 1
        } catch (ServiceNotFoundException $e) {
102 1
            $handler = new $className();
103
        }
104
105 1
        $callback[0] = $handler;
106
107 1
        return $callback;
108
    }
109
}
110