SymfonyRouteRouter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 39
dl 0
loc 87
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A match() 0 21 4
A addRoute() 0 24 2
A generateUri() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WShafer\Mezzio\Symfony\Router;
6
7
use Mezzio\Router\Route;
8
use Mezzio\Router\RouteResult;
9
use Mezzio\Router\RouterInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
12
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
13
use Symfony\Component\Routing\Generator\UrlGenerator;
14
use Symfony\Component\Routing\Matcher\UrlMatcher;
15
use Symfony\Component\Routing\RouteCollection;
16
use Symfony\Component\Routing\Route as SymfonyRoute;
17
use WShafer\Mezzio\Symfony\Router\Cache\Cache;
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
    public function __construct(
38
        RouteCollection $collection,
39
        UrlMatcher $urlMatcher,
40
        UrlGenerator $generator,
41
        Cache $cache
42
    ) {
43
        $this->collection = $collection;
44
        $this->urlMatcher = $urlMatcher;
45
        $this->generator = $generator;
46
        $this->cache = $cache;
47
48
        $this->cache->populateCollectionFromCache($collection);
49
    }
50
51
    public function addRoute(Route $route): void
52
    {
53
        $path = $route->getPath();
54
        $name = $route->getName();
55
56
        $this->routes[$name] = $route;
57
58
        if ($this->cache->has($name)) {
59
            return;
60
        }
61
62
        $symfonyRoute = new SymfonyRoute(
63
            $path,
64
            ['route' => $name],
65
            [],
66
            $route->getOptions(),
67
            null,
68
            [],
69
            $route->getAllowedMethods(),
70
            null
71
        );
72
73
        $this->cache->add($name, $symfonyRoute);
74
        $this->collection->add($name, $symfonyRoute);
75
    }
76
77
    /**
78
     * @SuppressWarnings(PHPMD.StaticAccess)
79
     */
80
    public function match(Request $request): RouteResult
81
    {
82
        $this->cache->writeCache();
83
84
        try {
85
            $match = $this->urlMatcher->match($request->getUri()->getPath());
86
        } catch (MethodNotAllowedException $e) {
87
            return RouteResult::fromRouteFailure($e->getAllowedMethods());
88
        } catch (ResourceNotFoundException $e) {
89
            return RouteResult::fromRouteFailure(null);
90
        }
91
92
        $route = $match['route'];
93
94
        if (empty($this->routes[$route])) {
95
            $this->cache->invalidateCacheFile();
96
            return RouteResult::fromRouteFailure(null);
97
        }
98
99
        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