Completed
Push — master ( df91db...e1615c )
by Simonas
13:43 queued 07:21
created

DocumentUrlGenerator::getCollector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
16
use Symfony\Component\Routing\Exception\RouteNotFoundException;
17
use Symfony\Component\Routing\Route;
18
19
class DocumentUrlGenerator extends ProviderBasedGenerator
20
{
21
    /**
22
     * @var array Route map configuration to map Elasticsearch types and Controllers.
23
     */
24
    private $routeMap;
25
26
    /**
27
     * @var MetadataCollector
28
     */
29
    private $collector;
30
31
    /**
32
     * @return mixed
33
     */
34
    public function getRouteMap()
35
    {
36
        return $this->routeMap;
37
    }
38
39
    /**
40
     * @param mixed $routeMap
41
     */
42
    public function setRouteMap($routeMap)
43
    {
44
        $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...
45
    }
46
47
    /**
48
     * @return MetadataCollector
49
     */
50
    public function getCollector()
51
    {
52
        return $this->collector;
53
    }
54
55
    /**
56
     * @param MetadataCollector $collector
57
     */
58
    public function setCollector(MetadataCollector $collector)
59
    {
60
        $this->collector = $collector;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
67
    {
68
        try {
69
            $document = $parameters['document'];
70
71
            if (is_object($document)) {
72
                $documentUrl = $document->url;
73
            } else {
74
                $documentUrl = $document['url'];
75
            }
76
77
            $type = $this->collector->getDocumentType(get_class($document));
78
            $route = new Route(
79
                $documentUrl,
80
                [
81
                    '_controller' => $this->routeMap[$type],
82
                    'document' => $document,
83
                    'type' => $type,
84
                ]
85
            );
86
87
            // the Route has a cache of its own and is not recompiled as long as it does not get modified
88
            $compiledRoute = $route->compile();
89
            $hostTokens = $compiledRoute->getHostTokens();
90
91
            $debug_message = $this->getRouteDebugMessage($name);
92
93
            return $this->doGenerate(
94
                $compiledRoute->getVariables(),
95
                $route->getDefaults(),
96
                $route->getRequirements(),
97
                $compiledRoute->getTokens(),
98
                $parameters,
99
                $debug_message,
100
                $referenceType,
101
                $hostTokens
102
            );
103
104
        } catch (\Exception $e) {
105
            throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
106
        }
107
    }
108
}
109