Completed
Push — master ( 4e1a65...7e3127 )
by Gabor
15:09
created

FastRouteAdapter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 9
c 2
b 1
f 1
lcom 1
cbo 6
dl 0
loc 87
ccs 40
cts 40
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 2
A getApplicationRouteUri() 0 10 3
B match() 0 22 4
1
<?php
2
/**
3
 * WebHemi
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 * @link      http://www.gixx-web.com
10
 */
11
12
namespace WebHemi\Adapter\Router\FastRoute;
13
14
use FastRoute\Dispatcher;
15
use FastRoute\RouteCollector;
16
use Psr\Http\Message\ServerRequestInterface;
17
use WebHemi\Adapter\Router\RouterAdapterInterface;
18
use WebHemi\Config\ConfigInterface;
19
use WebHemi\Routing\Result;
20
21
/**
22
 * Class FastRouteAdapter.
23
 */
24
class FastRouteAdapter implements RouterAdapterInterface
25
{
26
    /** @var Result */
27
    private $result;
28
    /** @var ConfigInterface */
29
    private $config;
30
    /** @var Dispatcher */
31
    private $adapter;
32
    /** @var string */
33
    private $applicationPath;
34
35
    /**
36
     * FastRouteAdapter constructor.
37
     *
38
     * @param Result          $routeResult
39
     * @param ConfigInterface $routeConfig
40
     * @param string          $applicationPath
41
     */
42 4
    public function __construct(Result $routeResult, ConfigInterface $routeConfig, $applicationPath = '/')
43
    {
44 4
        $this->result = $routeResult;
45 4
        $this->config = $routeConfig;
46 4
        $this->applicationPath = $applicationPath;
47
48 4
        $routes = $this->config->toArray();
49
50
        /** @var Dispatcher\GroupCountBase adapter */
51 4
        $this->adapter = \FastRoute\simpleDispatcher(
52 4
            function (RouteCollector $routeCollector) use ($routes) {
53 4
                foreach ($routes as $route) {
54 4
                    $method   = $route['allowed_methods'];
55 4
                    $uri      = $route['path'];
56 4
                    $callback = $route['middleware'];
57 4
                    $routeCollector->addRoute($method, $uri, $callback);
58 4
                }
59 4
            }
60 4
        );
61 4
    }
62
63
    /**
64
     * According to the application path, determines the route uri
65
     *
66
     * @param ServerRequestInterface $request
67
     *
68
     * @return string
69
     */
70 3
    private function getApplicationRouteUri(ServerRequestInterface $request)
71
    {
72 3
        $uri = $request->getUri()->getPath();
73
74 3
        if ($this->applicationPath != '/' && strpos($uri, $this->applicationPath) === 0) {
75 2
            $uri = substr($uri, strlen($this->applicationPath));
76 2
        }
77
78 3
        return $uri;
79
    }
80
81
    /**
82
     * Processes the Request and give a Result.
83
     *
84
     * @param ServerRequestInterface $request
85
     *
86
     * @return Result
87
     */
88 2
    public function match(ServerRequestInterface $request)
89
    {
90 2
        $httpMethod = $request->getMethod();
91 2
        $uri        = $this->getApplicationRouteUri($request);
92 2
        $routeInfo  = $this->adapter->dispatch($httpMethod, $uri);
93
94 2
        switch ($routeInfo[0]) {
95 2
            case Dispatcher::FOUND:
96 2
                $this->result->setStatus(Result::CODE_FOUND);
97 2
                $this->result->setMatchedMiddleware($routeInfo[1]);
98 2
                break;
99 2
            case Dispatcher::METHOD_NOT_ALLOWED:
100 2
                $this->result->setStatus(Result::CODE_BAD_METHOD);
101 2
                break;
102 2
            case Dispatcher::NOT_FOUND:
103 2
            default:
104 2
                $this->result->setStatus(Result::CODE_NOT_FOUND);
105 2
                break;
106 2
        }
107
108 2
        return $this->result;
109
    }
110
}
111