Completed
Push — master ( 5b9f4a...16a679 )
by Westin
01:49
created

SymfonyRouteRouter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 10
dl 0
loc 89
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B addRoute() 0 25 2
B match() 0 22 4
A generateUri() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WShafer\Expressive\Symfony\Router;
6
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
9
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
10
use Symfony\Component\Routing\Generator\UrlGenerator;
11
use Symfony\Component\Routing\Matcher\UrlMatcher;
12
use Symfony\Component\Routing\RouteCollection;
13
use Symfony\Component\Routing\Route as SymfonyRoute;
14
use WShafer\Expressive\Symfony\Router\Cache\Cache;
15
use Zend\Expressive\Router\Route;
16
use Zend\Expressive\Router\RouteResult;
17
use Zend\Expressive\Router\RouterInterface;
18
19
/**
20
 * @SuppressWarnings(PHPMD.LongVariable)
21
 */
22
class SymfonyRouteRouter implements RouterInterface
23
{
24
    /** @var RouteCollection  */
25
    protected $collection;
26
27
    /** @var UrlMatcher */
28
    protected $urlMatcher;
29
30
    /** @var UrlGenerator */
31
    protected $generator;
32
33
    protected $cache;
34
35
    protected $routes = [];
36
37 7
    public function __construct(
38
        RouteCollection $collection,
39
        UrlMatcher $urlMatcher,
40
        UrlGenerator $generator,
41
        Cache $cache
42
    ) {
43 7
        $this->collection = $collection;
44 7
        $this->urlMatcher = $urlMatcher;
45 7
        $this->generator = $generator;
46 7
        $this->cache = $cache;
47
48 7
        $this->cache->populateCollectionFromCache($collection);
49 7
    }
50
51 2
    public function addRoute(Route $route): void
52
    {
53 2
        $path = $route->getPath();
54 2
        $name = $route->getName();
55
56 2
        $this->routes[$name] = $route;
57
58 2
        if ($this->cache->has($name)) {
59 1
            return;
60
        }
61
62 1
        $symfonyRoute = new SymfonyRoute(
63 1
            $path,
64 1
            ['route' => $name],
65 1
            [],
66 1
            $route->getOptions(),
67 1
            null,
68 1
            [],
69 1
            $route->getAllowedMethods(),
0 ignored issues
show
Bug introduced by
It seems like $route->getAllowedMethods() targeting Zend\Expressive\Router\Route::getAllowedMethods() can also be of type null; however, Symfony\Component\Routing\Route::__construct() does only seem to accept string|array<integer,string>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70 1
            null
71
        );
72
73 1
        $this->cache->add($name, $symfonyRoute);
74 1
        $this->collection->add($name, $symfonyRoute);
75 1
    }
76
77
    /**
78
     * @SuppressWarnings(PHPMD.StaticAccess)
79
     */
80 4
    public function match(Request $request): RouteResult
81
    {
82 4
        $this->cache->writeCache();
83
84
        try {
85 4
            $match = $this->urlMatcher->match($request->getUri()->getPath());
86 2
        } catch (MethodNotAllowedException $e) {
87 1
            return RouteResult::fromRouteFailure($e->getAllowedMethods());
88 1
        } catch (ResourceNotFoundException $e) {
89 1
            return RouteResult::fromRouteFailure(null);
90
        }
91
92 2
        $route = $match['route'];
93
94 2
        if (empty($this->routes[$route])) {
95 1
            $this->cache->invalidateCacheFile();
96 1
            return RouteResult::fromRouteFailure(null);
97
        }
98
99 1
        unset($match['route'], $match['_route']);
100 1
        return RouteResult::fromRoute($this->routes[$route], $match);
101
    }
102
103
    /**
104
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
105
     */
106 1
    public function generateUri(string $name, array $substitutions = [], array $options = []): string
107
    {
108 1
        return $this->generator->generate($name, $substitutions);
109
    }
110
}
111