Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

MetaRouter::supports()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 5
nc 9
nop 1
crap 6
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
    /**
29
     * @param string|Meta     $name
30
     * @param array           $parameters
31
     * @param bool|int|string $referenceType
32
     *
33
     * @return mixed|string
34
     *
35
     * @throws RouteNotFoundException
36
     */
37 75
    public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
38
    {
39 75
        $cacheKey = $this->getCacheKey($name, $parameters, $referenceType);
40 75
        if (array_key_exists($cacheKey, $this->internalRoutesCache)) {
41 2
            return $this->internalRoutesCache[$cacheKey];
42
        }
43
44 75
        if ($name instanceof Meta && $name->getValues() instanceof ArticleInterface) {
45 4
            $parameters['slug'] = $name->getValues()->getSlug();
46 4
            $route = $name->getValues()->getRoute();
47
48 4
            if (null === $route && $name->getContext()->getCurrentPage()) {
49 1
                $parameters['slug'] = null;
50 4
                $route = $name->getContext()->getCurrentPage()->getValues();
51
            }
52 71
        } elseif ($name instanceof Meta && $name->getValues() instanceof RouteInterface) {
53
            $route = $name->getValues();
54
        } else {
55 71
            $route = $name;
56
        }
57
58 75
        if (null === $route) {
59
            throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
60
        }
61
62 75
        $result = parent::generate($route, $parameters, $referenceType);
0 ignored issues
show
Bug introduced by
It seems like $route can also be of type array or object; however, Symfony\Cmf\Component\Ro...namicRouter::generate() does only seem to accept string|object<Symfony\Component\Routing\Route>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63 18
        $this->internalRoutesCache[$cacheKey] = $result;
64 18
        unset($route, $name, $parameters);
65
66 18
        return $result;
67
    }
68
69 75
    private function getCacheKey($route, $parameters, $type)
70
    {
71 75
        if ($route instanceof Meta && $route->getValues() instanceof ArticleInterface) {
72 4
            $name = $route->getValues()->getId();
73 71
        } elseif ($route instanceof Meta && $route->getValues() instanceof RouteInterface) {
74
            $name = $route->getValues()->getName();
75
        } elseif ($route instanceof RouteInterface) {
76 2
            $name = $route->getName();
77
        } else {
78 69
            $name = $route;
79
        }
80
81 75
        return md5($name.serialize($parameters).$type);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 79
    public function supports($name)
88
    {
89 79
        return ($name instanceof Meta && (
90 3
            $name->getValues() instanceof ArticleInterface ||
91 3
            $name->getValues() instanceof RouteInterface
92 79
        )) || $name instanceof RouteInterface || (is_string($name) && $name !== 'homepage');
93
    }
94
}
95