Passed
Push — master ( 33dbd6...cec097 )
by Nuno
04:05
created

Builder::count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended;
15
16
use function is_callable;
17
use Laravel\Scout\Builder as BaseBuilder;
18
19
final class Builder extends BaseBuilder
20
{
21
    /*
22
     * Count the number of items in the search results.
23
     *
24
     * @return int
25
     */
26 2
    public function count(): int
27
    {
28 2
        $raw = $this->raw();
29
30 2
        return array_key_exists('nbHits', $raw) ? (int) $raw['nbHits'] : 0;
0 ignored issues
show
Bug introduced by
It seems like $raw can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        return array_key_exists('nbHits', /** @scrutinizer ignore-type */ $raw) ? (int) $raw['nbHits'] : 0;
Loading history...
31
    }
32
33
    /*
34
     * Customize the search with the provided search parameters.
35
     *
36
     * @link https://www.algolia.com/doc/api-reference/search-api-parameters
37
     *
38
     * @param array $parameters The search parameters.
39
     *
40
     * @return \Algolia\ScoutExtended\Builder
41
     */
42 2
    public function with(array $parameters): Builder
43
    {
44 2
        $callback = $this->callback;
45
46
        $this->callback = function ($algolia, $query, $baseParameters) use ($parameters, $callback) {
0 ignored issues
show
Documentation Bug introduced by
It seems like function(...) { /* ... */ } of type callable is incompatible with the declared type string of property $callback.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47 2
            $parameters = array_merge($parameters, $baseParameters);
48
49 2
            if (is_callable($callback)) {
50
                return $callback($algolia, $query, $parameters);
51
            }
52
53 2
            return $algolia->search($query, $parameters);
54
        };
55
56 2
        return $this;
57
    }
58
59
    /*
60
     * Customize the search to be around a given location.
61
     *
62
     * @link https://www.algolia.com/doc/guides/geo-search/geo-search-overview
63
     *
64
     * @param float $lat Latitude of the center
65
     * @param float $lng Longitude of the center
66
     *
67
     * @return \Algolia\ScoutExtended\Builder
68
     */
69 1
    public function aroundLatLng(float $lat, float $lng): Builder
70
    {
71 1
        return $this->with([
72 1
            'aroundLatLng' => $lat.','.$lng,
73
        ]);
74
    }
75
}
76