Completed
Push — master ( 3428c1...e29901 )
by Simonas
23:11
created

DocumentUrlGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\RouterBundle\Routing;
13
14
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
15
use Symfony\Component\Routing\Generator\UrlGenerator;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
use Symfony\Component\Routing\Route as SymfonyRoute;
18
use Symfony\Component\Routing\RequestContext;
19
use Symfony\Component\Routing\Exception\RouteNotFoundException;
20
use Psr\Log\LoggerInterface;
21
22
class DocumentUrlGenerator extends UrlGenerator implements VersatileGeneratorInterface
23
{
24
    /**
25
     * The route provider for this generator.
26
     *
27
     * @var RouteProviderInterface
28
     */
29
    protected $provider;
30
31
    /**
32
     * @param RouteProviderInterface $provider
33
     * @param LoggerInterface        $logger
34
     */
35
    public function __construct(RouteProviderInterface $provider, LoggerInterface $logger = null)
36
{
37
    $this->provider = $provider;
38
    $this->logger = $logger;
39
    $this->context = new RequestContext();
40
}
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function generate($name, $parameters = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
46
{
47
    if ($name instanceof SymfonyRoute) {
48
        $route = $name;
49
    } elseif (null === $route = $this->provider->getRouteByName($name, $parameters)) {
50
        throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
51
    }
52
53
    // the Route has a cache of its own and is not recompiled as long as it does not get modified
54
    $compiledRoute = $route->compile();
55
    $hostTokens = $compiledRoute->getHostTokens();
56
57
    $debug_message = $this->getRouteDebugMessage($name);
58
59
    return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $absolute, $hostTokens);
60
}
61
62
    /**
63
     * Support a route object and any string as route name.
64
     *
65
     * {@inheritdoc}
66
     */
67
    public function supports($name)
68
{
69
    return is_string($name) || $name instanceof SymfonyRoute;
70
}
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getRouteDebugMessage($name, array $parameters = array())
76
{
77
    if (is_scalar($name)) {
78
        return $name;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $name; of type integer|double|string|boolean is incompatible with the return type declared by the interface Symfony\Cmf\Component\Ro...e::getRouteDebugMessage of type string as it can also be of type boolean which is not included in this return type.
Loading history...
79
    }
80
81
    if (is_array($name)) {
82
        return serialize($name);
83
    }
84
85
    if ($name instanceof RouteObjectInterface) {
0 ignored issues
show
Bug introduced by
The class ONGR\RouterBundle\Routing\RouteObjectInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
86
        return 'Route with key '.$name->getRouteKey();
87
    }
88
89
    if ($name instanceof SymfonyRoute) {
90
        return 'Route with path '.$name->getPath();
91
    }
92
93
    return get_class($name);
94
}
95
}