Passed
Push — master ( 00b675...b5f4af )
by Gabor
06:20
created

ServiceAdapter::__construct()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 16
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * WebHemi
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 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
declare(strict_types = 1);
12
13
namespace WebHemi\Router\ServiceAdapter\FastRoute;
14
15
use FastRoute\Dispatcher;
16
use FastRoute\RouteCollector;
17
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
18
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
19
use WebHemi\Http\ServerRequestInterface;
20
use WebHemi\Router\Result\Result;
21
use WebHemi\Router\ServiceInterface;
22
23
/**
24
 * Class ServiceAdapter.
25
 */
26
class ServiceAdapter implements ServiceInterface
27
{
28
    /** @var Result */
29
    private $result;
30
    /** @var Dispatcher\GroupCountBased */
31
    private $adapter;
32
    /** @var string */
33
    private $applicationPath;
34
35
    /**
36
     * ServiceAdapter constructor.
37
     *
38
     * @param ConfigurationInterface $configuration
39
     * @param EnvironmentInterface   $environmentManager
40
     * @param Result                 $routeResult
41
     */
42 9
    public function __construct(
43
        ConfigurationInterface $configuration,
44
        EnvironmentInterface $environmentManager,
45
        Result $routeResult
46
    ) {
47 9
        $module = $environmentManager->getSelectedModule();
48 9
        $routes = $configuration->getData('router/'.$module);
49
50 9
        $this->result = $routeResult;
51 9
        $this->applicationPath = $environmentManager->getSelectedApplicationUri();
52 9
        $this->adapter = \FastRoute\simpleDispatcher(
53 9
            function (RouteCollector $routeCollector) use ($routes) {
54 9
                foreach ($routes as $resource => $route) {
55 9
                    $resourceName = $route['resource_name'] ?? $resource;
56
57 9
                    $method   = $route['allowed_methods'];
58 9
                    $uri      = $route['path'];
59 9
                    $callback = $route['middleware'].'@'.$resourceName;
60
61 9
                    $routeCollector->addRoute($method, $uri, $callback);
62
                }
63 9
            }
64
        );
65 9
    }
66
67
    /**
68
     * According to the application path, determines the route uri
69
     *
70
     * @param ServerRequestInterface $request
71
     * @return string
72
     */
73 8
    private function getApplicationRouteUri(ServerRequestInterface $request) : string
74
    {
75 8
        $uri = $request->getUri()->getPath();
76
77 8
        if ($this->applicationPath != '/' && strpos($uri, $this->applicationPath) === 0) {
78 2
            $uri = substr($uri, strlen($this->applicationPath));
79
        }
80
81 8
        return empty($uri) ? '/' : $uri;
82
    }
83
84
    /**
85
     * Processes the Request and give a Result.
86
     *
87
     * @param ServerRequestInterface $request
88
     * @return Result
89
     */
90 7
    public function match(ServerRequestInterface $request) : Result
91
    {
92 7
        $httpMethod = $request->getMethod();
93 7
        $uri        = $this->getApplicationRouteUri($request);
94 7
        $routeInfo  = $this->adapter->dispatch($httpMethod, $uri);
95 7
        $result     = clone $this->result;
96
97 7
        switch ($routeInfo[0]) {
98 7
            case Dispatcher::FOUND:
99 5
                list($middleware, $resource) = explode('@', $routeInfo[1]);
100 5
                $result->setStatus(Result::CODE_FOUND);
101 5
                $result->setMatchedMiddleware($middleware);
102 5
                $result->setParameters($routeInfo[2]);
103 5
                $result->setResource($resource);
104 5
                break;
105 4
            case Dispatcher::METHOD_NOT_ALLOWED:
106 3
                $result->setStatus(Result::CODE_BAD_METHOD);
107 3
                break;
108 3
            case Dispatcher::NOT_FOUND:
109
            default:
110 3
                $result->setStatus(Result::CODE_NOT_FOUND);
111 3
                break;
112
        }
113
114 7
        return $result;
115
    }
116
}
117