Completed
Pull Request — master (#1160)
by
unknown
05:50
created

TransformedFinder::createRawPaginatorAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
c 1
b 0
f 0
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace FOS\ElasticaBundle\Finder;
4
5
use Elastica\Document;
6
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
7
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
8
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
9
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
10
use Pagerfanta\Pagerfanta;
11
use Elastica\SearchableInterface;
12
use Elastica\Query;
13
14
/**
15
 * Finds elastica documents and map them to persisted objects.
16
 */
17
class TransformedFinder implements PaginatedFinderInterface
18
{
19
    protected $searchable;
20
    protected $transformer;
21
22
    /**
23
     * @param SearchableInterface                 $searchable
24
     * @param ElasticaToModelTransformerInterface $transformer
25
     */
26 8
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
27
    {
28 8
        $this->searchable  = $searchable;
29 8
        $this->transformer = $transformer;
30 8
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function find($query, $limit = null, $options = array())
36
    {
37 1
        $results = $this->search($query, $limit, $options);
38
39 1
        return $this->transformer->transform($results);
40
    }
41
42 1
    public function findHybrid($query, $limit = null, $options = array())
43
    {
44 1
        $results = $this->search($query, $limit, $options);
45
46 1
        return $this->transformer->hybridTransform($results);
47
    }
48
49
    /**
50
     * Find documents similar to one with passed id.
51
     *
52
     * @param integer $id
53
     * @param array   $params
54
     * @param array   $query
55
     *
56
     * @return array of model objects
57
     **/
58 1
    public function moreLikeThis($id, $params = array(), $query = array())
59
    {
60 1
        $doc = new Document($id);
61 1
        $results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults();
0 ignored issues
show
Bug introduced by
The method moreLikeThis() does not seem to exist on object<Elastica\SearchableInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
63 1
        return $this->transformer->transform($results);
64
    }
65
66
    /**
67
     * @param $query
68
     * @param null|int $limit
69
     * @param array    $options
70
     *
71
     * @return array
72
     */
73 1
    protected function search($query, $limit = null, $options = array())
74
    {
75 1
        $queryObject = Query::create($query);
76 1
        if (null !== $limit) {
77 1
            $queryObject->setSize($limit);
78
        }
79 1
        $results = $this->searchable->search($queryObject, $options)->getResults();
80
81 1
        return $results;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function findPaginated($query, $options = array())
88
    {
89 1
        $queryObject = Query::create($query);
90 1
        $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
91
92 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 2
    public function createPaginatorAdapter($query, $options = array())
99
    {
100 2
        $query = Query::create($query);
101
102 2
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 1
    public function createHybridPaginatorAdapter($query)
109
    {
110 1
        $query = Query::create($query);
111
112 1
        return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function createRawPaginatorAdapter($query)
119
    {
120
        $query = Query::create($query);
121
122
        return new RawPaginatorAdapter($this->searchable, $query);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \FOS\Elastica...s->searchable, $query); (FOS\ElasticaBundle\Finder\RawPaginatorAdapter) is incompatible with the return type declared by the interface FOS\ElasticaBundle\Finde...eateRawPaginatorAdapter of type FOS\ElasticaBundle\Pagin...ginatorAdapterInterface.

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...
123
    }
124
}
125