Completed
Pull Request — master (#1109)
by Karel
20:30 queued 17:31
created

RawPaginatorAdapter::getResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace FOS\ElasticaBundle\Paginator;
4
5
use Elastica\SearchableInterface;
6
use Elastica\Query;
7
use Elastica\ResultSet;
8
use InvalidArgumentException;
9
10
/**
11
 * Allows pagination of Elastica\Query. Does not map results.
12
 */
13
class RawPaginatorAdapter implements PaginatorAdapterInterface
14
{
15
    /**
16
     * @var SearchableInterface the object to search in
17
     */
18
    private $searchable;
19
20
    /**
21
     * @var Query the query to search
22
     */
23
    private $query;
24
25
    /**
26
     * @var array search options
27
     */
28
    private $options;
29
30
    /**
31
     * @var integer the number of hits
32
     */
33
    private $totalHits;
34
35
    /**
36
     * @var array for the facets
37
     */
38
    private $facets;
39
40
    /**
41
     * @var array for the aggregations
42
     */
43
    private $aggregations;
44
45
    /**
46
     * @see PaginatorAdapterInterface::__construct
47
     *
48
     * @param SearchableInterface $searchable the object to search in
49
     * @param Query               $query      the query to search
50
     * @param array               $options
51
     */
52 1
    public function __construct(SearchableInterface $searchable, Query $query, array $options = array())
53
    {
54 1
        $this->searchable = $searchable;
55 1
        $this->query      = $query;
56 1
        $this->options    = $options;
57 1
    }
58
59
    /**
60
     * Returns the paginated results.
61
     *
62
     * @param integer $offset
63
     * @param integer $itemCountPerPage
64
     *
65
     * @throws \InvalidArgumentException
66
     *
67
     * @return ResultSet
68
     */
69
    protected function getElasticaResults($offset, $itemCountPerPage)
70
    {
71
        $offset = (integer) $offset;
72
        $itemCountPerPage = (integer) $itemCountPerPage;
73
        $size = $this->query->hasParam('size')
74
            ? (integer) $this->query->getParam('size')
75
            : null;
76
77
        if (null !== $size && $size < $offset + $itemCountPerPage) {
78
            $itemCountPerPage = $size - $offset;
79
        }
80
81
        if ($itemCountPerPage < 1) {
82
            throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
83
        }
84
85
        $query = clone $this->query;
86
        $query->setFrom($offset);
87
        $query->setSize($itemCountPerPage);
88
89
        $resultSet = $this->searchable->search($query, $this->options);
90
        $this->totalHits = $resultSet->getTotalHits();
91
92
        if (method_exists($resultSet, 'getFacets')) {
93
            $this->facets = $resultSet->getFacets();
0 ignored issues
show
Deprecated Code introduced by
The method Elastica\ResultSet::getFacets() has been deprecated with message: Facets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
        }
95
        
96
        $this->aggregations = $resultSet->getAggregations();
97
98
        return $resultSet;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getResults($offset, $itemCountPerPage)
105
    {
106
        return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
107
    }
108
109
    /**
110
     * Returns the number of results.
111
     *
112
     * If genuineTotal is provided as true, total hits is returned from the
113
     * hits.total value from the search results instead of just returning
114
     * the requested size.
115
     *
116
     * {@inheritdoc}
117
     */
118
    public function getTotalHits($genuineTotal = false)
119
    {
120
        if (! isset($this->totalHits)) {
121
            $this->totalHits = $this->searchable->count($this->query);
122
        }
123
124
        return $this->query->hasParam('size') && !$genuineTotal
125
            ? min($this->totalHits, (integer) $this->query->getParam('size'))
126
            : $this->totalHits;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getFacets()
133
    {
134
        if (! isset($this->facets)) {
135
            $this->facets = $this->searchable->search($this->query)->getFacets();
0 ignored issues
show
Deprecated Code introduced by
The method Elastica\ResultSet::getFacets() has been deprecated with message: Facets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
136
        }
137
138
        return $this->facets;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getAggregations()
145
    {
146
        if (!isset($this->aggregations)) {
147
            $this->aggregations = $this->searchable->search($this->query)->getAggregations();
148
        }
149
150
        return $this->aggregations;
151
    }
152
153
    /**
154
     * Returns the Query.
155
     *
156
     * @return Query the search query
157
     */
158
    public function getQuery()
159
    {
160
        return $this->query;
161
    }
162
}
163