Completed
Push — master ( 22fced...7ac2cc )
by Karel
04:33
created

RawPaginatorAdapter::getSuggests()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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\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
     * @see PaginatorAdapterInterface::__construct
56
     *
57
     * @param SearchableInterface $searchable the object to search in
58
     * @param Query               $query      the query to search
59
     * @param array               $options
60
     */
61 3
    public function __construct(SearchableInterface $searchable, Query $query, array $options = array())
62
    {
63 3
        $this->searchable = $searchable;
64 3
        $this->query = $query;
65 3
        $this->options = $options;
66 3
    }
67
68
    /**
69
     * Returns the paginated results.
70
     *
71
     * @param int $offset
72
     * @param int $itemCountPerPage
73
     *
74
     * @throws \InvalidArgumentException
75
     *
76
     * @return ResultSet
77
     */
78
    protected function getElasticaResults($offset, $itemCountPerPage)
79
    {
80
        $offset = (int) $offset;
81
        $itemCountPerPage = (int) $itemCountPerPage;
82
        $size = $this->query->hasParam('size')
83
            ? (int) $this->query->getParam('size')
84
            : null;
85
86
        if (null !== $size && $size < $offset + $itemCountPerPage) {
87
            $itemCountPerPage = $size - $offset;
88
        }
89
90
        if ($itemCountPerPage < 1) {
91
            throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
92
        }
93
94
        $query = clone $this->query;
95
        $query->setFrom($offset);
96
        $query->setSize($itemCountPerPage);
97
98
        $resultSet = $this->searchable->search($query, $this->options);
99
        $this->totalHits = $resultSet->getTotalHits();
100
        $this->aggregations = $resultSet->getAggregations();
101
        $this->suggests = $resultSet->getSuggests();
102
103
        return $resultSet;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getResults($offset, $itemCountPerPage)
110
    {
111
        return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
112
    }
113
114
    /**
115
     * Returns the number of results.
116
     *
117
     * If genuineTotal is provided as true, total hits is returned from the
118
     * hits.total value from the search results instead of just returning
119
     * the requested size.
120
     *
121
     * {@inheritdoc}
122
     */
123
    public function getTotalHits($genuineTotal = false)
124
    {
125
        if (!isset($this->totalHits)) {
126
            $this->totalHits = $this->searchable->count($this->query);
127
        }
128
129
        return $this->query->hasParam('size') && !$genuineTotal
130
            ? min($this->totalHits, (int) $this->query->getParam('size'))
131
            : $this->totalHits;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getAggregations()
138
    {
139
        if (!isset($this->aggregations)) {
140
            $this->aggregations = $this->searchable->search($this->query)->getAggregations();
141
        }
142
143
        return $this->aggregations;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getSuggests()
150
    {
151
        if (!isset($this->suggests)) {
152
            $this->suggests = $this->searchable->search($this->query)->getSuggests();
153
        }
154
155
        return $this->suggests;
156
    }
157
158
    /**
159
     * Returns the Query.
160
     *
161
     * @return Query the search query
162
     */
163
    public function getQuery()
164
    {
165
        return $this->query;
166
    }
167
}
168