Completed
Push — master ( b0b358...8400ab )
by
unknown
02:50
created

ElasticaAdapter::getSlice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Pagerfanta project.
5
 *
6
 * (c) Tim Nagel <[email protected]>
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 Pagerfanta\Adapter;
13
14
use Elastica\Query;
15
use Elastica\SearchableInterface;
16
17
class ElasticaAdapter implements AdapterInterface
18
{
19
    /**
20
     * @var Query
21
     */
22
    private $query;
23
24
    /**
25
     * @var \Elastica\ResultSet
26
     */
27
    private $resultSet;
28
29
    /**
30
     * @var SearchableInterface
31
     */
32
    private $searchable;
33
34
    /**
35
     * @var array
36
     */
37
    private $options;
38
39 3
    public function __construct(SearchableInterface $searchable, Query $query, array $options = array())
40
    {
41 3
        $this->searchable = $searchable;
42 3
        $this->query = $query;
43 3
        $this->options = $options;
44 3
    }
45
46
    /**
47
     * Returns the number of results.
48
     *
49
     * @return integer The number of results.
50
     */
51 1
    public function getNbResults()
52
    {
53 1
        if (!$this->resultSet) {
54 1
            return $this->searchable->search($this->query, $this->options)->getTotalHits();
55
        }
56
57
        return $this->resultSet->getTotalHits();
58
    }
59
60
    /**
61
     * Returns the Elastica ResultSet. Will return null if getSlice has not yet been
62
     * called.
63
     *
64
     * @return \Elastica\ResultSet|null
65
     */
66 2
    public function getResultSet()
67
    {
68 2
        return $this->resultSet;
69
    }
70
71
    /**
72
     * Returns an slice of the results.
73
     *
74
     * @param integer $offset The offset.
75
     * @param integer $length The length.
76
     *
77
     * @return array|\Traversable The slice.
78
     */
79 2
    public function getSlice($offset, $length)
80
    {
81 2
        return $this->resultSet = $this->searchable->search($this->query, array_merge($this->options, array(
82 2
            'from' => $offset,
83
            'size' => $length
84 2
        )));
85
    }
86
}
87