Completed
Push — master ( d6bb8c...11242d )
by Simonas
9s
created

DocumentUrlGenerator::nameHandle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
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
        if ($name != 'ongr_route') {
74
            throw new RouteNotFoundException('Route ' . $name . ' is not a valid name: make sure the name is ongr_route');
75
        }
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
82
    {
83
        try {
84
            $document = $parameters['document'];
85
            $this->nameHandle($name);
86
            if (is_object($document)) {
87
                $documentUrl = $document->url;
88
            } else {
89
                $documentUrl = $document['url'];
90
            }
91
92
            $type = $this->collector->getDocumentType(get_class($document));
93
            $route = new Route(
94
                $documentUrl,
95
                [
96
                    '_controller' => $this->routeMap[$type],
97
                    'document' => $document,
98
                    'type' => $type,
99
                ]
100
            );
101
102
            // the Route has a cache of its own and is not recompiled as long as it does not get modified
103
            $compiledRoute = $route->compile();
104
            $hostTokens = $compiledRoute->getHostTokens();
105
106
            $debug_message = $this->getRouteDebugMessage($name);
107
108
            return $this->doGenerate(
109
                $compiledRoute->getVariables(),
110
                $route->getDefaults(),
111
                $route->getRequirements(),
112
                $compiledRoute->getTokens(),
113
                $parameters,
114
                $debug_message,
115
                $referenceType,
116
                $hostTokens
117
            );
118
119
        } catch (\Exception $e) {
120
            throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
121
        }
122
    }
123
}
124