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

ElasticsearchRouteProvider::getRouteMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
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 ONGR\ElasticsearchBundle\Result\Result;
16
use ONGR\ElasticsearchBundle\Service\Manager;
17
use ONGR\ElasticsearchDSL\Query\MatchQuery;
18
use ONGR\ElasticsearchDSL\Search;
19
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\Routing\Exception\RouteNotFoundException;
22
use Symfony\Component\Routing\Route;
23
use Symfony\Component\Routing\RouteCollection;
24
25
class ElasticsearchRouteProvider implements RouteProviderInterface
26
{
27
    /**
28
     * @var array Route map configuration to map Elasticsearch types and Controllers.
29
     */
30
    private $routeMap;
31
32
    /**
33
     * @var Manager
34
     */
35
    private $manager;
36
37
    /**
38
     * @var MetadataCollector
39
     */
40
    private $collector;
41
42
    /**
43
     * ElasticsearchRouteProvider constructor.
44
     *
45
     * @param MetadataCollector $collector
46
     * @param array $routeMap
47
     */
48
    public function __construct($collector, array $routeMap = [])
49
    {
50
        $this->collector = $collector;
51
        $this->routeMap = $routeMap;
52
    }
53
54
    /**
55
     * Returns Elasticsearch manager instance that was set in app/config.yml.
56
     *
57
     * @return Manager
58
     */
59
    public function getManager()
60
    {
61
        return $this->manager;
62
    }
63
64
    /**
65
     * @param Manager $manager
66
     */
67
    public function setManager(Manager $manager)
68
    {
69
        $this->manager = $manager;
70
    }
71
    /**
72
     * @return array
73
     */
74
    public function getRouteMap()
75
    {
76
        return $this->routeMap;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function getRouteCollectionForRequest(Request $request)
83
    {
84
        if (!$this->manager) {
85
            throw new \Exception('Manager must be set to execute query to the elasticsearch');
86
        }
87
88
        $routeCollection = new RouteCollection();
89
        $requestPath = $request->getPathInfo();
90
91
        $search = new Search();
92
        $search->addQuery(new MatchQuery('url', $requestPath));
93
94
        $results = $this->manager->execute(array_keys($this->routeMap), $search, Result::RESULTS_OBJECT);
95
        try {
96
            foreach ($results as $document) {
97
                $type = $this->collector->getDocumentType(get_class($document));
98
                if (array_key_exists($type, $this->routeMap)) {
99
                    $route = new Route(
100
                        $document->url,
101
                        [
102
                            '_controller' => $this->routeMap[$type],
103
                            'document' => $document,
104
                            'type' => $type,
105
                        ]
106
                    );
107
108
                    $routeCollection->add('ongr_route_' . $route->getDefault('type'), $route);
109
                } else {
110
                    throw new RouteNotFoundException(sprintf('Route for type %s% cannot be generated.', $type));
111
                }
112
            }
113
        } catch (\Exception $e) {
114
            throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
115
        }
116
117
        return $routeCollection;
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function getRouteByName($name)
124
    {
125
        throw new RouteNotFoundException('Dynamic provider generates routes on the fly.');
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131
    public function getRoutesByNames($names)
132
    {
133
        // Returns empty Route collection.
134
        return new RouteCollection();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...ting\RouteCollection(); (Symfony\Component\Routing\RouteCollection) is incompatible with the return type declared by the interface Symfony\Cmf\Component\Ro...rface::getRoutesByNames of type Symfony\Component\Routing\Route[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
135
    }
136
}
137