TransformedFinder::search()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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 9
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
39
    {
40 9
        $this->searchable = $searchable;
41 9
        $this->transformer = $transformer;
42 9
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function find($query, ?int $limit = null, array $options = [])
48
    {
49 1
        $results = $this->search($query, $limit, $options);
50
51 1
        return $this->transformer->transform($results);
52
    }
53
54
    /**
55
     * @param $query
56
     *
57
     * @return array
58
     */
59 1
    public function findHybrid($query, ?int $limit = null, array $options = [])
60
    {
61 1
        $results = $this->search($query, $limit, $options);
62
63 1
        return $this->transformer->hybridTransform($results);
64
    }
65
66
    /**
67
     * @param $query
68
     */
69 1
    public function findRaw($query, ?int $limit = null, array $options = []): array
70
    {
71 1
        return $this->search($query, $limit, $options);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 1
    public function findPaginated($query, array $options = [])
78
    {
79 1
        $paginatorAdapter = $this->createPaginatorAdapter($query, $options);
80
81 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
82
    }
83
84
    /**
85
     * Searches for query hybrid results and returns them wrapped in a paginator.
86
     *
87
     * @param mixed $query Can be a string, an array or an \Elastica\Query object
88
     *
89
     * @return Pagerfanta paginated hybrid results
90
     */
91 1
    public function findHybridPaginated($query, array $options = [])
92
    {
93 1
        $paginatorAdapter = $this->createHybridPaginatorAdapter($query, $options);
94
95 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    public function createPaginatorAdapter($query, array $options = [])
102
    {
103 2
        $query = Query::create($query);
104
105 2
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function createHybridPaginatorAdapter($query, array $options = [])
112
    {
113 2
        $query = Query::create($query);
114
115 2
        return new HybridPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function createRawPaginatorAdapter($query, array $options = [])
122
    {
123
        $query = Query::create($query);
124
125
        return new RawPaginatorAdapter($this->searchable, $query, $options);
126
    }
127
128
    /**
129
     * @param $query
130
     *
131
     * @return array
132
     */
133 1
    protected function search($query, ?int $limit = null, array $options = [])
134
    {
135 1
        $queryObject = Query::create($query);
136 1
        if (null !== $limit) {
137 1
            $queryObject->setSize($limit);
138
        }
139 1
        $results = $this->searchable->search($queryObject, $options)->getResults();
140
141 1
        return $results;
142
    }
143
}
144