Completed
Pull Request — master (#1567)
by
unknown
04:03
created

TransformedFinder::findHybridPaginated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\Query;
15
use Elastica\SearchableInterface;
16
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
17
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
18
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter;
19
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
20
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
21
use Pagerfanta\Pagerfanta;
22
23
/**
24
 * Finds elastica documents and map them to persisted objects.
25
 */
26
class TransformedFinder implements PaginatedFinderInterface
27
{
28
    /**
29
     * @var SearchableInterface
30
     */
31
    protected $searchable;
32
33
    /**
34
     * @var ElasticaToModelTransformerInterface
35
     */
36
    protected $transformer;
37
38
    /**
39
     * @param SearchableInterface                 $searchable
40
     * @param ElasticaToModelTransformerInterface $transformer
41
     */
42 7
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
43
    {
44 7
        $this->searchable = $searchable;
45 7
        $this->transformer = $transformer;
46 7
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function find($query, $limit = null, $options = [])
52
    {
53 1
        $results = $this->search($query, $limit, $options);
54
55 1
        return $this->transformer->transform($results);
56
    }
57
58
    /**
59
     * @param $query
60
     * @param null|int $limit
61
     * @param array    $options
62
     *
63
     * @return array
64
     */
65 1
    public function findHybrid($query, $limit = null, $options = [])
66
    {
67 1
        $results = $this->search($query, $limit, $options);
68
69 1
        return $this->transformer->hybridTransform($results);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function findPaginated($query, $options = [])
76
    {
77 1
        $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
0 ignored issues
show
Bug introduced by
The variable $queryObject does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
78
79
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function findHybridPaginated($query)
86
    {
87
        $paginatorAdapter = $this->createHybridPaginatorAdapter($query);
88
89
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function createPaginatorAdapter($query, $options = [])
96
    {
97 1
        $query = Query::create($query);
98
99 1
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function createHybridPaginatorAdapter($query)
106
    {
107 1
        $query = Query::create($query);
108
109 1
        return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function createRawPaginatorAdapter($query, $options = [])
116
    {
117
        $query = Query::create($query);
118
119
        return new RawPaginatorAdapter($this->searchable, $query, $options);
120
    }
121
122
    /**
123
     * @param $query
124
     * @param null|int $limit
125
     * @param array    $options
126
     *
127
     * @return array
128
     */
129 1
    protected function search($query, $limit = null, $options = [])
130
    {
131 1
        $queryObject = Query::create($query);
132 1
        if (null !== $limit) {
133 1
            $queryObject->setSize($limit);
134
        }
135 1
        $results = $this->searchable->search($queryObject, $options)->getResults();
136
137 1
        return $results;
138
    }
139
}
140