Completed
Pull Request — master (#63)
by Simonas
07:11
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
15
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
16
use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
17
use Symfony\Component\Routing\Exception\RouteNotFoundException;
18
use Symfony\Component\Routing\Route;
19
20
class DocumentUrlGenerator extends ProviderBasedGenerator
21
{
22
    /**
23
     * @var array Route map configuration to map Elasticsearch types and Controllers.
24
     */
25
    private $routeMap;
26
27
    /**
28
     * @var MetadataCollector
29
     */
30
    private $collector;
31
32
    /**
33
     * @return mixed
34
     */
35
    public function getRouteMap()
36
    {
37
        return $this->routeMap;
38
    }
39
40
    /**
41
     * @param mixed $routeMap
42
     */
43
    public function setRouteMap($routeMap)
44
    {
45
        $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...
46
    }
47
48
    /**
49
     * @return MetadataCollector
50
     */
51
    public function getCollector()
52
    {
53
        return $this->collector;
54
    }
55
56
    /**
57
     * @param MetadataCollector $collector
58
     */
59
    public function setCollector(MetadataCollector $collector)
60
    {
61
        $this->collector = $collector;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
68
    {
69
        try {
70
            $document = $parameters['document'];
71
72
            if (is_object($document)) {
73
                $documentUrl = $document->url;
74
            } else {
75
                $documentUrl = $document['url'];
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($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens);
95
96
        } catch (\Exception $e) {
97
            throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
98
        }
99
    }
100
}