Test Failed
Push — develop ( 3663ad...1f9f00 )
by Nuno
04:47
created

BuilderMacros   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 26.32%

Importance

Changes 0
Metric Value
wmc 6
eloc 19
dl 0
loc 75
ccs 5
cts 19
cp 0.2632
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A aroundLatLng() 0 24 2
A with() 0 23 2
A count() 0 11 2
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 Closure;
17
use Laravel\Scout\Builder;
18
use function call_user_func;
19
20
/**
21
 * @internal
22
 */
23
final class BuilderMacros
24
{
25
    /**
26
     * @return \Closure
27
     */
28 18
    public function count(): Closure
29
    {
30
        /*
31
         * Count the number of items in the search results.
32
         *
33
         * @return int
34
         */
35
        return function (): int {
36
            $raw = $this->engine()->search($this);
0 ignored issues
show
Bug introduced by
The method engine() does not exist on Algolia\ScoutExtended\BuilderMacros. ( Ignorable by Annotation )

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

36
            $raw = $this->/** @scrutinizer ignore-call */ engine()->search($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38
            return array_key_exists('nbHits', $raw) ? (int) $raw['nbHits'] : 0;
39 18
        };
40
    }
41
42
    /**
43
     * @return \Closure
44
     */
45 18
    public function aroundLatLng(): Closure
46
    {
47
        /*
48
         * Search for entries around a given location.
49
         *
50
         * @link https://www.algolia.com/doc/guides/geo-search/geo-search-overview/
51
         *
52
         * @param float $lat Latitude of the center
53
         * @param float $lng Longitude of the center
54
         *
55
         * @return \Laravel\Scout\Builder
56
         */
57
        return function (float $lat, float $lng): Builder {
58
            $callback = $this->callback;
59
            $this->callback = function ($algolia, $query, $options) use ($lat, $lng, $callback) {
0 ignored issues
show
Bug Best Practice introduced by
The property callback does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
                $options['aroundLatLng'] = (float) $lat.','.(float) $lng;
61
                if ($callback) {
62
                    return call_user_func($callback, $algolia, $query, $options);
63
                }
64
65
                return $algolia->search($query, $options);
66
            };
67
68
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Algolia\ScoutExtended\BuilderMacros which is incompatible with the type-hinted return Laravel\Scout\Builder.
Loading history...
69 18
        };
70
    }
71
72
    /**
73
     * @return \Closure
74
     */
75 18
    public function with(): Closure
76
    {
77
        /*
78
         * Customize the request with the provided options
79
         *
80
         * @param array $options Latitude of the center
81
         *
82
         * @return \Laravel\Scout\Builder
83
         */
84
        return function (array $options): Builder {
85
            $callback = $this->callback;
86
87
            $this->callback = function ($algolia, $query, $baseOptions) use ($options, $callback) {
0 ignored issues
show
Bug Best Practice introduced by
The property callback does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
88
                $options = array_merge($options, $baseOptions);
89
90
                if ($callback) {
91
                    return call_user_func($callback, $algolia, $query, $options);
92
                }
93
94
                return $algolia->search($query, $options);
95
            };
96
97
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Algolia\ScoutExtended\BuilderMacros which is incompatible with the type-hinted return Laravel\Scout\Builder.
Loading history...
98 18
        };
99
    }
100
}
101