Passed
Push — develop ( 359c84...5df010 )
by Brent
04:09
created

Router::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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