Completed
Pull Request — master (#1160)
by
unknown
04:40
created

TransformedFinder::createPaginatorAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\ElasticaBundle\Finder;
13
14
use Elastica\Document;
15
use Elastica\Query;
16
use Elastica\SearchableInterface;
17
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
18
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
19
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
20
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter;
21
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
22
use Pagerfanta\Pagerfanta;
23
24
/**
25
 * Finds elastica documents and map them to persisted objects.
26
 */
27
class TransformedFinder implements PaginatedFinderInterface
28
{
29
    protected $searchable;
30
    protected $transformer;
31
32
    /**
33
     * @param SearchableInterface                 $searchable
34
     * @param ElasticaToModelTransformerInterface $transformer
35
     */
36 8
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
37
    {
38 8
        $this->searchable = $searchable;
39 8
        $this->transformer = $transformer;
40 8
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function find($query, $limit = null, $options = array())
46
    {
47 1
        $results = $this->search($query, $limit, $options);
48
49 1
        return $this->transformer->transform($results);
50
    }
51
52 1
    public function findHybrid($query, $limit = null, $options = array())
53
    {
54 1
        $results = $this->search($query, $limit, $options);
55
56 1
        return $this->transformer->hybridTransform($results);
57
    }
58
59
    /**
60
     * Find documents similar to one with passed id.
61
     *
62
     * @param int   $id
63
     * @param array $params
64
     * @param array $query
65
     *
66
     * @return array of model objects
67
     **/
68 1
    public function moreLikeThis($id, $params = array(), $query = array())
69
    {
70 1
        $doc = new Document($id);
71 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...
72
73 1
        return $this->transformer->transform($results);
74
    }
75
76
    /**
77
     * @param $query
78
     * @param null|int $limit
79
     * @param array    $options
80
     *
81
     * @return array
82
     */
83 1
    protected function search($query, $limit = null, $options = array())
84
    {
85 1
        $queryObject = Query::create($query);
86 1
        if (null !== $limit) {
87 1
            $queryObject->setSize($limit);
88
        }
89 1
        $results = $this->searchable->search($queryObject, $options)->getResults();
90
91 1
        return $results;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    public function findPaginated($query, $options = array())
98
    {
99 1
        $queryObject = Query::create($query);
100 1
        $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
101
102 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    public function createPaginatorAdapter($query, $options = array())
109
    {
110 2
        $query = Query::create($query);
111
112 2
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function createHybridPaginatorAdapter($query)
119
    {
120 1
        $query = Query::create($query);
121
122 1
        return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function createRawPaginatorAdapter($query)
129
    {
130
        $query = Query::create($query);
131
132
        return new RawPaginatorAdapter($this->searchable, $query);
133
    }
134
}
135