Completed
Pull Request — master (#72)
by
unknown
12:36
created

DocumentUrlGenerator::setRouteMap()   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 1
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
     * Checks if the $name is a valid string for a route
65
     * @param $name
66
     * @throws RouteNotFoundException
67
     */
68
    private function nameHandle($name)
69
    {
70
        if (!is_string($name)) {
71
            throw new RouteNotFoundException('Route ' . $name . ' is not a string');
72
        }
73
        $contains = false;
74
        foreach ($this->provider->getRouteMap() as $key => $value) {
75
            if ($name == 'ongr_route_' . $key) {
76
                $contains = true;
77
            }
78
            if(!$contains){
79
                throw new RouteNotFoundException('Route ' . $name . ' is not a valid name: make sure the name is ongr_route_<type-name>');
80
            }
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
88
    {
89
        try {
90
            $document = $parameters['document'];
91
            $this->nameHandle($name);
92
            if (is_object($document)) {
93
                $documentUrl = $document->url;
94
            } else {
95
                $documentUrl = $document['url'];
96
            }
97
98
            $type = $this->collector->getDocumentType(get_class($document));
99
            $route = new Route(
100
                $documentUrl,
101
                [
102
                    '_controller' => $this->routeMap[$type],
103
                    'document' => $document,
104
                    'type' => $type,
105
                ]
106
            );
107
108
            // the Route has a cache of its own and is not recompiled as long as it does not get modified
109
            $compiledRoute = $route->compile();
110
            $hostTokens = $compiledRoute->getHostTokens();
111
112
            $debug_message = $this->getRouteDebugMessage($name);
113
114
            return $this->doGenerate(
115
                $compiledRoute->getVariables(),
116
                $route->getDefaults(),
117
                $route->getRequirements(),
118
                $compiledRoute->getTokens(),
119
                $parameters,
120
                $debug_message,
121
                $referenceType,
122
                $hostTokens
123
            );
124
125
        } catch (\Exception $e) {
126
            throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
127
        }
128
    }
129
}
130