Completed
Push — master ( fdc077...aa5351 )
by Karel
16s
created

TransformedFinder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 130
ccs 30
cts 33
cp 0.9091
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 6 1
A findHybrid() 0 6 1
A findRaw() 0 4 1
A findPaginated() 0 6 1
A findHybridPaginated() 0 6 1
A createPaginatorAdapter() 0 6 1
A createHybridPaginatorAdapter() 0 6 1
A createRawPaginatorAdapter() 0 6 1
A search() 0 10 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 9
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
43
    {
44 9
        $this->searchable = $searchable;
45 9
        $this->transformer = $transformer;
46 9
    }
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
     * @param $query
74
     * @param null|int $limit
75
     * @param array    $options
76
     *
77
     * @return array
78
     */
79 1
    public function findRaw($query, ?int $limit = null, array $options = []): array
80
    {
81 1
        return $this->search($query, $limit, $options);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function findPaginated($query, $options = [])
88
    {
89 1
        $paginatorAdapter = $this->createPaginatorAdapter($query, $options);
90
91 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
92
    }
93
94
    /**
95
     * Searches for query hybrid results and returns them wrapped in a paginator.
96
     *
97
     * @param mixed $query Can be a string, an array or an \Elastica\Query object
98
     *
99
     * @return Pagerfanta paginated hybrid results
100
     */
101 1
    public function findHybridPaginated($query)
102
    {
103 1
        $paginatorAdapter = $this->createHybridPaginatorAdapter($query);
104
105 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function createPaginatorAdapter($query, $options = [])
112
    {
113 2
        $query = Query::create($query);
114
115 2
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2
    public function createHybridPaginatorAdapter($query)
122
    {
123 2
        $query = Query::create($query);
124
125 2
        return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function createRawPaginatorAdapter($query, $options = [])
132
    {
133
        $query = Query::create($query);
134
135
        return new RawPaginatorAdapter($this->searchable, $query, $options);
136
    }
137
138
    /**
139
     * @param $query
140
     * @param null|int $limit
141
     * @param array    $options
142
     *
143
     * @return array
144
     */
145 1
    protected function search($query, $limit = null, $options = [])
146
    {
147 1
        $queryObject = Query::create($query);
148 1
        if (null !== $limit) {
149 1
            $queryObject->setSize($limit);
150
        }
151 1
        $results = $this->searchable->search($queryObject, $options)->getResults();
152
153 1
        return $results;
154
    }
155
}
156