Completed
Push — master ( e31a71...bd9098 )
by Westin
02:03
created

SymfonyRouteRouter::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3
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\Exception\RouteNotFoundException;
11
use Symfony\Component\Routing\Generator\UrlGenerator;
12
use Symfony\Component\Routing\Matcher\UrlMatcher;
13
use Symfony\Component\Routing\RouteCollection;
14
use Symfony\Component\Routing\Route as SymfonyRoute;
15
use WShafer\Expressive\Symfony\Router\Cache\Cache;
16
use Zend\Expressive\Router\Route;
17
use Zend\Expressive\Router\RouteResult;
18
use Zend\Expressive\Router\RouterInterface;
19
20
/**
21
 * @SuppressWarnings(PHPMD.LongVariable)
22
 */
23
class SymfonyRouteRouter implements RouterInterface
24
{
25
    /** @var RouteCollection  */
26
    protected $collection;
27
28
    /** @var UrlMatcher */
29
    protected $urlMatcher;
30
31
    /** @var UrlGenerator */
32
    protected $generator;
33
34
    protected $cache;
35
36 6
    public function __construct(
37
        RouteCollection $collection,
38
        UrlMatcher $urlMatcher,
39
        UrlGenerator $generator,
40
        Cache $cache
41
    ) {
42 6
        $this->collection = $collection;
43 6
        $this->urlMatcher = $urlMatcher;
44 6
        $this->generator = $generator;
45 6
        $this->cache = $cache;
46
47 6
        $this->cache->populateCollectionFromCache($collection);
48 6
    }
49
50 1
    public function addRoute(Route $route): void
51
    {
52 1
        $path = $route->getPath();
53 1
        $name = $route->getName();
54
55 1
        if ($this->cache->has($name)) {
56
            return;
57
        }
58
59 1
        $symfonyRoute = new SymfonyRoute(
60 1
            $path,
61 1
            ['route' => $route],
62 1
            [],
63 1
            $route->getOptions(),
64 1
            null,
65 1
            [],
66 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...
67 1
            null
68
        );
69
70 1
        $this->cache->add($name, $symfonyRoute);
71 1
        $this->collection->add($name, $symfonyRoute);
72 1
    }
73
74
    /**
75
     * @SuppressWarnings(PHPMD.StaticAccess)
76
     */
77 3
    public function match(Request $request): RouteResult
78
    {
79 3
        $this->cache->writeCache();
80
81
        try {
82 3
            $match = $this->urlMatcher->match($request->getUri()->getPath());
83 2
        } catch (MethodNotAllowedException $e) {
84 1
            return RouteResult::fromRouteFailure($e->getAllowedMethods());
85 1
        } catch (ResourceNotFoundException $e) {
86 1
            return RouteResult::fromRouteFailure(null);
87
        }
88
89 1
        $route = $match['route'];
90 1
        unset($match['route'], $match['_route']);
91 1
        return RouteResult::fromRoute($route, $match);
92
    }
93
94
    /**
95
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
96
     */
97 1
    public function generateUri(string $name, array $substitutions = [], array $options = []): string
98
    {
99 1
        return $this->generator->generate($name, $substitutions);
100
    }
101
}
102