Passed
Push — master ( ab5fc8...24a035 )
by Nuno
05:30
created

Builder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 function func_num_args;
18
use Laravel\Scout\Builder as BaseBuilder;
19
20
final class Builder extends BaseBuilder
21
{
22
    /**
23
     * {@inheritdoc}
24
     *
25
     * @see https://github.com/algolia/scout-extended/issues/98
26
     */
27 16
    public function __construct($model, $query, $callback = null, $softDelete = false)
28
    {
29 16
        parent::__construct($model, (string) $query, $callback, $softDelete);
30 16
    }
31
32
    /**
33
     * Customize the search to be around a given location.
34
     *
35
     * @link https://www.algolia.com/doc/guides/geo-search/geo-search-overview
36
     *
37
     * @param float $lat Latitude of the center
38
     * @param float $lng Longitude of the center
39
     *
40
     * @return $this
41
     */
42 1
    public function aroundLatLng(float $lat, float $lng): self
43
    {
44 1
        return $this->with([
45 1
            'aroundLatLng' => $lat.','.$lng,
46
        ]);
47
    }
48
49
    /**
50
     * Count the number of items in the search results.
51
     *
52
     * @return int
53
     */
54 2
    public function count(): int
55
    {
56 2
        $raw = $this->raw();
57
58 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

58
        return array_key_exists('nbHits', /** @scrutinizer ignore-type */ $raw) ? (int) $raw['nbHits'] : 0;
Loading history...
59
    }
60
61
    /**
62
     * Customize the search adding a where clause.
63
     *
64
     * @param  string $field
65
     * @param  mixed $operator
66
     * @param  mixed $value
67
     *
68
     * @return $this
69
     */
70 6
    public function where($field, $operator, $value = null): self
71
    {
72
        // Here we will make some assumptions about the operator. If only 2 values are
73
        // passed to the method, we will assume that the operator is an equals sign
74
        // and keep going. Otherwise, we'll require the operator to be passed in.
75 6
        if (func_num_args() === 2) {
76 4
            return parent::where($field, $this->transform($operator));
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::where($fi...->transform($operator)) returns the type Laravel\Scout\Builder which includes types incompatible with the type-hinted return Algolia\ScoutExtended\Builder.
Loading history...
77
        }
78
79 2
        return parent::where($field, "$operator {$this->transform($value)}");
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::where($fi...his->transform($value)) returns the type Laravel\Scout\Builder which includes types incompatible with the type-hinted return Algolia\ScoutExtended\Builder.
Loading history...
80
    }
81
82
    /**
83
     * Customize the search adding a where between clause.
84
     *
85
     * @param  string $field
86
     * @param  array $values
87
     *
88
     * @return $this
89
     */
90 2
    public function whereBetween($field, array $values): self
91
    {
92 2
        return $this->where("$field:", "{$this->transform($values[0])} TO {$this->transform($values[1])}");
93
    }
94
95
    /**
96
     * Customize the search with the provided search parameters.
97
     *
98
     * @link https://www.algolia.com/doc/api-reference/search-api-parameters
99
     *
100
     * @param array $parameters The search parameters.
101
     *
102
     * @return $this
103
     */
104 2
    public function with(array $parameters): self
105
    {
106 2
        $callback = $this->callback;
107
108
        $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...
109 2
            $parameters = array_merge($parameters, $baseParameters);
110
111 2
            if (is_callable($callback)) {
112
                return $callback($algolia, $query, $parameters);
113
            }
114
115 2
            return $algolia->search($query, $parameters);
116
        };
117
118 2
        return $this;
119
    }
120
121
    /**
122
     * Transform the given where value.
123
     *
124
     * @param  mixed $value
125
     *
126
     * @return mixed
127
     */
128 6
    private function transform($value)
129
    {
130
        /*
131
         * Casts carbon instances to timestamp.
132
         */
133 6
        if ($value instanceof \Illuminate\Support\Carbon) {
134 2
            $value = $value->getTimestamp();
135
        }
136
137 6
        return $value;
138
    }
139
}
140