Completed
Pull Request — master (#1246)
by Istvan
04:23
created

RawPaginatorAdapter::getMaxScore()   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
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
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
     * @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
     * @param array               $options
65
     */
66 3
    public function __construct(SearchableInterface $searchable, Query $query, array $options = [])
67
    {
68 3
        $this->searchable = $searchable;
69 3
        $this->query = $query;
70 3
        $this->options = $options;
71 3
    }
72
73
    /**
74
     * Returns the paginated results.
75
     *
76
     * @param int $offset
77
     * @param int $itemCountPerPage
78
     *
79
     * @throws \InvalidArgumentException
80
     *
81
     * @return ResultSet
82
     */
83
    protected function getElasticaResults($offset, $itemCountPerPage)
84
    {
85
        $offset = (int) $offset;
86
        $itemCountPerPage = (int) $itemCountPerPage;
87
        $size = $this->query->hasParam('size')
88
            ? (int) $this->query->getParam('size')
89
            : null;
90
91
        if (null !== $size && $size < $offset + $itemCountPerPage) {
92
            $itemCountPerPage = $size - $offset;
93
        }
94
95
        if ($itemCountPerPage < 1) {
96
            throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
97
        }
98
99
        $query = clone $this->query;
100
        $query->setFrom($offset);
101
        $query->setSize($itemCountPerPage);
102
103
        $resultSet = $this->searchable->search($query, $this->options);
104
        $this->totalHits = $resultSet->getTotalHits();
105
        $this->aggregations = $resultSet->getAggregations();
106
        $this->suggests = $resultSet->getSuggests();
107
        $this->maxScore = $resultSet->getMaxScore();
0 ignored issues
show
Documentation Bug introduced by
It seems like $resultSet->getMaxScore() can also be of type integer. However, the property $maxScore is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
108
109
        return $resultSet;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getResults($offset, $itemCountPerPage)
116
    {
117
        return new RawPartialResults($this->getElasticaResults($offset, $itemCountPerPage));
118
    }
119
120
    /**
121
     * Returns the number of results.
122
     *
123
     * If genuineTotal is provided as true, total hits is returned from the
124
     * hits.total value from the search results instead of just returning
125
     * the requested size.
126
     *
127
     * {@inheritdoc}
128
     */
129
    public function getTotalHits($genuineTotal = false)
130
    {
131
        if (!isset($this->totalHits)) {
132
            $this->totalHits = $this->searchable->count($this->query);
133
        }
134
135
        return $this->query->hasParam('size') && !$genuineTotal
136
            ? min($this->totalHits, (int) $this->query->getParam('size'))
137
            : $this->totalHits;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getAggregations()
144
    {
145
        if (!isset($this->aggregations)) {
146
            $this->aggregations = $this->searchable->search($this->query)->getAggregations();
147
        }
148
149
        return $this->aggregations;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function getSuggests()
156
    {
157
        if (!isset($this->suggests)) {
158
            $this->suggests = $this->searchable->search($this->query)->getSuggests();
159
        }
160
161
        return $this->suggests;
162
    }
163
164
    /**
165
     * @return float
166
     */
167
    public function getMaxScore()
168
    {
169
        if (!isset($this->maxScore)) {
170
            $this->maxScore = $this->searchable->search($this->query)->getMaxScore();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->searchable->searc...->query)->getMaxScore() can also be of type integer. However, the property $maxScore is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
171
        }
172
173
        return $this->maxScore;
174
    }
175
176
    /**
177
     * Returns the Query.
178
     *
179
     * @return Query the search query
180
     */
181
    public function getQuery()
182
    {
183
        return $this->query;
184
    }
185
}
186