Completed
Pull Request — master (#1229)
by Jorge
05:22
created

RawPaginatorAdapter   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 11.9%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 149
ccs 5
cts 42
cp 0.119
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getElasticaResults() 0 30 6
A getResults() 0 4 1
A getTotalHits() 0 10 4
A getAggregations() 0 8 2
A getSuggests() 0 8 2
A getQuery() 0 4 1
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 = [])
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
        // Override size if count was already done.
87
        $size = $this->totalHits ?: $size;
88
89
        if (null !== $size && $size < $offset + $itemCountPerPage) {
90
            $itemCountPerPage = $size - $offset;
91
        }
92
93
        if ($itemCountPerPage < 1) {
94
            throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
95
        }
96
97
        $query = clone $this->query;
98
        $query->setFrom($offset);
99
        $query->setSize($itemCountPerPage);
100
101
        $resultSet = $this->searchable->search($query, $this->options);
102
        $this->totalHits = $resultSet->getTotalHits();
103
        $this->aggregations = $resultSet->getAggregations();
104
        $this->suggests = $resultSet->getSuggests();
105
106
        return $resultSet;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getResults($offset, $itemCountPerPage)
113
    {
114
        return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
115
    }
116
117
    /**
118
     * Returns the number of results.
119
     *
120
     * If genuineTotal is provided as true, total hits is returned from the
121
     * hits.total value from the search results instead of just returning
122
     * the requested size.
123
     *
124
     * {@inheritdoc}
125
     */
126
    public function getTotalHits($genuineTotal = false)
127
    {
128
        if (!isset($this->totalHits)) {
129
            $this->totalHits = $this->searchable->count($this->query);
130
        }
131
132
        return $this->query->hasParam('size') && !$genuineTotal
133
            ? min($this->totalHits, (int) $this->query->getParam('size'))
134
            : $this->totalHits;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getAggregations()
141
    {
142
        if (!isset($this->aggregations)) {
143
            $this->aggregations = $this->searchable->search($this->query)->getAggregations();
144
        }
145
146
        return $this->aggregations;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getSuggests()
153
    {
154
        if (!isset($this->suggests)) {
155
            $this->suggests = $this->searchable->search($this->query)->getSuggests();
156
        }
157
158
        return $this->suggests;
159
    }
160
161
    /**
162
     * Returns the Query.
163
     *
164
     * @return Query the search query
165
     */
166
    public function getQuery()
167
    {
168
        return $this->query;
169
    }
170
}
171