Passed
Push — develop ( 3db2fb...b27e40 )
by Septianata
05:11
created

QueryScope   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 15
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 10
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A scopeNearestLocation() 0 19 1
1
<?php
2
3
namespace App\Models\Concerns\Branch;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
/**
8
 * @method static \Illuminate\Database\Eloquent\Builder|static nearestLocation()
9
 *
10
 * @see \App\Models\Branch
11
 * @see \Illuminate\Database\Eloquent\Builder @callScope()
12
 */
13
trait QueryScope
14
{
15
    /**
16
     * Scope a query to find nearest location based on the given latitude and longitude.
17
     *
18
     * @param  \Illuminate\Database\Eloquent\Builder  $query
19
     * @param  mixed  $latitude
20
     * @param  mixed  $longitude
21
     * @return \Illuminate\Database\Eloquent\Builder
22
     */
23
    public function scopeNearestLocation(Builder $query, $latitude, $longitude)
24
    {
25
        $fieldLatitude = 'address_latitude';
26
        $fieldLongitude = 'address_longitude';
27
28
        $selectQuery = sprintf(
29
            '*, ' .
30
            '6371 * acos(cos(radians(?)) ' .
31
            '* cos(radians(%s)) ' .
32
            '* cos(radians(%s) - radians(?)) ' .
33
            '+ sin(radians(?)) ' .
34
            '* sin(radians(%s))) AS distance',
35
            $fieldLatitude, $fieldLongitude, $fieldLatitude
36
        );
37
38
        return $query->selectRaw(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->selectRaw...))->orderBy('distance') also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Builder.
Loading history...
39
            $selectQuery,
40
            [$latitude, $longitude, $latitude]
41
        )->orderBy('distance');
42
    }
43
}
44