Completed
Pull Request — master (#75)
by
unknown
01:54
created

DocumentUrlGenerator::getRouteDebugMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
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 ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
15
use ONGR\RouterBundle\Document\SeoAwareInterface;
16
use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
17
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
18
use Symfony\Component\Routing\Exception\RouteNotFoundException;
19
use Symfony\Component\Routing\Route;
20
21
class DocumentUrlGenerator extends ProviderBasedGenerator implements VersatileGeneratorInterface
22
{
23
    /**
24
     * @var array Route map configuration to map Elasticsearch types and Controllers.
25
     */
26
    private $routeMap;
27
28
    /**
29
     * @var MetadataCollector
30
     */
31
    private $collector;
32
33
    /**
34
     * @return mixed
35
     */
36
    public function getRouteMap()
37
    {
38
        return $this->routeMap;
39
    }
40
41
    /**
42
     * @param mixed $routeMap
43
     */
44
    public function setRouteMap($routeMap)
45
    {
46
        $this->routeMap = $routeMap;
0 ignored issues
show
Documentation Bug introduced by
It seems like $routeMap of type * is incompatible with the declared type array of property $routeMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    /**
50
     * @return MetadataCollector
51
     */
52
    public function getCollector()
53
    {
54
        return $this->collector;
55
    }
56
57
    /**
58
     * @param MetadataCollector $collector
59
     */
60
    public function setCollector(MetadataCollector $collector)
61
    {
62
        $this->collector = $collector;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
69
    {
70
        try {
71
            $document = $name;
72
            if ($document instanceof SeoAwareInterface) {
73
                $documentUrl = $document->getUrl();
74
            } else {
75
                throw new RouteNotFoundException();
76
            }
77
78
            $type = $this->collector->getDocumentType(get_class($document));
79
            $route = new Route(
80
                $documentUrl,
81
                [
82
                    '_controller' => $this->routeMap[$type],
83
                    'document' => $document,
84
                    'type' => $type,
85
                ]
86
            );
87
88
            // the Route has a cache of its own and is not recompiled as long as it does not get modified
89
            $compiledRoute = $route->compile();
90
            $hostTokens = $compiledRoute->getHostTokens();
91
92
            $debug_message = $this->getRouteDebugMessage($name);
93
94
            return $this->doGenerate(
95
                $compiledRoute->getVariables(),
96
                $route->getDefaults(),
97
                $route->getRequirements(),
98
                $compiledRoute->getTokens(),
99
                $parameters,
100
                $debug_message,
101
                $referenceType,
102
                $hostTokens
103
            );
104
        } catch (\Exception $e) {
105
            throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
106
        }
107
    }
108
    /**
109
     * @param mixed $name The route "name" which may also be an object or anything
110
     *
111
     * @return bool
112
     * @throws RouteNotFoundException
113
     */
114
    public function supports($name)
115
    {
116
        if ($name instanceof SeoAwareInterface) {
117
            return true;
118
        } else {
119
            throw new RouteNotFoundException('$name must be an instance of SeoAwareInterface');
120
        }
121
    }
122
    /**
123
     * @param mixed $name
124
     * @param array $parameters which should contain a content field containing
125
     *                          a RouteReferrersReadInterface object
126
     *
127
     * @return string
128
     */
129
    public function getRouteDebugMessage($name, array $parameters = array())
130
    {
131
        if ($name instanceof SeoAwareInterface) {
132
            return 'The route object is fit for parsing to generate() method';
133
        } else {
134
            return $name.' must be an instance of SeoAwareInterface';
135
        }
136
    }
137
}
138