Completed
Push — master ( bd9098...f08431 )
by Westin
10:15
created

SymfonyRouteRouter::match()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
crap 4
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 6
37
    public function __construct(
38
        RouteCollection $collection,
39
        UrlMatcher $urlMatcher,
40
        UrlGenerator $generator,
41
        Cache $cache
42 6
    ) {
43 6
        $this->collection = $collection;
44 6
        $this->urlMatcher = $urlMatcher;
45 6
        $this->generator = $generator;
46
        $this->cache = $cache;
47 6
48 6
        $this->cache->populateCollectionFromCache($collection);
49
    }
50 1
51
    public function addRoute(Route $route): void
52 1
    {
53 1
        $path = $route->getPath();
54
        $name = $route->getName();
55 1
56
        $this->routes[$name] = $route;
57
58
        if ($this->cache->has($name)) {
59 1
            return;
60 1
        }
61 1
62 1
        $symfonyRoute = new SymfonyRoute(
63 1
            $path,
64 1
            ['route' => $name],
65 1
            [],
66 1
            $route->getOptions(),
67 1
            null,
68
            [],
69
            $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 1
        );
72 1
73
        $this->cache->add($name, $symfonyRoute);
74
        $this->collection->add($name, $symfonyRoute);
75
    }
76
77 3
    /**
78
     * @SuppressWarnings(PHPMD.StaticAccess)
79 3
     */
80
    public function match(Request $request): RouteResult
81
    {
82 3
        $this->cache->writeCache();
83 2
84 1
        try {
85 1
            $match = $this->urlMatcher->match($request->getUri()->getPath());
86 1
        } catch (MethodNotAllowedException $e) {
87
            return RouteResult::fromRouteFailure($e->getAllowedMethods());
88
        } catch (ResourceNotFoundException $e) {
89 1
            return RouteResult::fromRouteFailure(null);
90 1
        }
91 1
92
        $route = $match['route'];
93
94
        if (!$this->routes[$route]) {
95
            $this->cache->invalidateCacheFile();
96
            return RouteResult::fromRouteFailure(null);
97 1
        }
98
99 1
        unset($match['route'], $match['_route']);
100
        return RouteResult::fromRoute($this->routes[$route], $match);
101
    }
102
103
    /**
104
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
105
     */
106
    public function generateUri(string $name, array $substitutions = [], array $options = []): string
107
    {
108
        return $this->generator->generate($name, $substitutions);
109
    }
110
}
111