RawPaginatorAdapter::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Paginator;
13
14
use Elastica\Query;
15
use Elastica\ResultSet;
16
use Elastica\SearchableInterface;
17
use InvalidArgumentException;
18
19
/**
20
 * Allows pagination of Elastica\Query. Does not map results.
21
 */
22
class RawPaginatorAdapter implements PaginatorAdapterInterface
23
{
24
    /**
25
     * @var SearchableInterface the object to search in
26
     */
27
    private $searchable;
28
29
    /**
30
     * @var Query the query to search
31
     */
32
    private $query;
33
34
    /**
35
     * @var array search options
36
     */
37
    private $options;
38
39
    /**
40
     * @var int the number of hits
41
     */
42
    private $totalHits;
43
44
    /**
45
     * @var array for the aggregations
46
     */
47
    private $aggregations;
48
49
    /**
50
     * @var array for the suggesters
51
     */
52
    private $suggests;
53
54
    /**
55
     * @var float
56
     */
57
    private $maxScore;
58
59
    /**
60
     * @see PaginatorAdapterInterface::__construct
61
     *
62
     * @param SearchableInterface $searchable the object to search in
63
     * @param Query               $query      the query to search
64
     */
65 11
    public function __construct(SearchableInterface $searchable, Query $query, array $options = [])
66
    {
67 11
        $this->searchable = $searchable;
68 11
        $this->query = $query;
69 11
        $this->options = $options;
70 11
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getResults($offset, $itemCountPerPage)
76
    {
77
        return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
78
    }
79
80
    /**
81
     * Returns the number of results.
82
     *
83
     * If genuineTotal is provided as true, total hits is returned from the
84
     * hits.total value from the search results instead of just returning
85
     * the requested size.
86
     *
87
     * {@inheritdoc}
88
     */
89 2
    public function getTotalHits($genuineTotal = false)
90
    {
91 2
        if (!isset($this->totalHits)) {
92 2
            $this->totalHits = $this->searchable->count($this->query);
93
        }
94
95 2
        return $this->query->hasParam('size') && !$genuineTotal
96 1
            ? \min($this->totalHits, (int) $this->query->getParam('size'))
97 2
            : $this->totalHits;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function getAggregations()
104
    {
105 1
        if (!isset($this->aggregations)) {
106 1
            $this->aggregations = $this->searchable->search($this->query)->getAggregations();
107
        }
108
109 1
        return $this->aggregations;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function getSuggests()
116
    {
117 1
        if (!isset($this->suggests)) {
118 1
            $this->suggests = $this->searchable->search($this->query)->getSuggests();
119
        }
120
121 1
        return $this->suggests;
122
    }
123
124
    /**
125
     * @return float
126
     */
127 1
    public function getMaxScore()
128
    {
129 1
        if (!isset($this->maxScore)) {
130 1
            $this->maxScore = $this->searchable->search($this->query)->getMaxScore();
131
        }
132
133 1
        return $this->maxScore;
134
    }
135
136
    /**
137
     * Returns the Query.
138
     *
139
     * @return Query the search query
140
     */
141 1
    public function getQuery()
142
    {
143 1
        return $this->query;
144
    }
145
146
    /**
147
     * Returns the paginated results.
148
     *
149
     * @param int $offset
150
     * @param int $itemCountPerPage
151
     *
152
     * @throws \InvalidArgumentException
153
     *
154
     * @return ResultSet
155
     */
156
    protected function getElasticaResults($offset, $itemCountPerPage)
157
    {
158
        $offset = (int) $offset;
159
        $itemCountPerPage = (int) $itemCountPerPage;
160
        $size = $this->query->hasParam('size')
161
            ? (int) $this->query->getParam('size')
162
            : null;
163
164
        if (null !== $size && $size < $offset + $itemCountPerPage) {
165
            $itemCountPerPage = $size - $offset;
166
        }
167
168
        if ($itemCountPerPage < 1) {
169
            throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
170
        }
171
172
        $query = clone $this->query;
173
        $query->setFrom($offset);
174
        $query->setSize($itemCountPerPage);
175
176
        $resultSet = $this->searchable->search($query, $this->options);
177
        $this->totalHits = $resultSet->getTotalHits();
178
        $this->aggregations = $resultSet->getAggregations();
179
        $this->suggests = $resultSet->getSuggests();
180
        $this->maxScore = $resultSet->getMaxScore();
181
182
        return $resultSet;
183
    }
184
}
185