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

SymfonyRouteRouter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 97.22%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 10
dl 0
loc 79
ccs 35
cts 36
cp 0.9722
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A addRoute() 0 23 2
A match() 0 16 3
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\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