Completed
Push — master ( 24abed...91ab53 )
by Rafał
15:03
created

MetaRouter::supports()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 11
nop 1
crap 7
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Routing;
16
17
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
18
use SWP\Bundle\ContentBundle\Model\RouteInterface;
19
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
20
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
21
use Symfony\Component\Routing\Exception\RouteNotFoundException;
22
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23
24
class MetaRouter extends DynamicRouter
25
{
26
    protected $internalRoutesCache = [];
27
28
    public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
29
    {
30
        $cacheKey = $this->getCacheKey($name, $parameters, $referenceType);
31
        if (array_key_exists($cacheKey, $this->internalRoutesCache)) {
32
            return $this->internalRoutesCache[$cacheKey];
33
        }
34
35
        $route = $name;
36
        if ($name instanceof Meta) {
37 86
            if ($name->getValues() instanceof ArticleInterface) {
38
                $parameters['slug'] = $name->getValues()->getSlug();
39 86
                $route = $name->getValues()->getRoute();
40 86
41 2
                if (null === $route && $name->getContext()->getCurrentPage()) {
42
                    $parameters['slug'] = null;
43
                    $route = $name->getContext()->getCurrentPage()->getValues();
44 86
                }
45 5
            } elseif ($name->getValues() instanceof RouteInterface) {
46 5
                $route = $name->getValues();
47
            }
48 5
        } elseif ($name instanceof ArticleInterface) {
49 1
            $route = $name->getRoute();
50 5
            $parameters['slug'] = $name->getSlug();
51
        }
52 82
53
        if (null === $route || is_array($route)) {
54
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
55 82
        }
56
57
        $result = parent::generate($route, $parameters, $referenceType);
58 86
        $this->internalRoutesCache[$cacheKey] = $result;
59
        unset($route);
60
61
        return $result;
62 86
    }
63 14
64 14
    private function getCacheKey($route, $parameters, $type)
65
    {
66 14
        $name = $route;
67
        if ($route instanceof Meta) {
68
            if ($route->getValues() instanceof ArticleInterface) {
69 86
                $name = $route->getValues()->getId();
70
            } elseif ($route->getValues() instanceof RouteInterface) {
71 86
                $name = $route->getValues()->getName();
72 5
            }
73 82
        } elseif ($route instanceof RouteInterface) {
74
            $name = $route->getName();
75
        } elseif ($route instanceof ArticleInterface) {
76 2
            $name = $route->getId();
77
        }
78 80
79
        return md5($name.serialize($parameters).$type);
80
    }
81 86
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function supports($name)
86
    {
87 86
        return ($name instanceof Meta && (
88
            $name->getValues() instanceof ArticleInterface ||
89 86
            $name->getValues() instanceof RouteInterface
90 4
        )) || $name instanceof RouteInterface || $name instanceof ArticleInterface || (is_string($name) && 'homepage' !== $name);
91 4
    }
92
}
93