Addressable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 69
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
deleted() 0 1 ?
morphMany() 0 1 ?
A bootAddressable() 0 6 1
A addresses() 0 4 1
A findByDistance() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Addresses\Traits;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Database\Eloquent\Relations\MorphMany;
9
10
trait Addressable
11
{
12
    /**
13
     * Register a deleted model event with the dispatcher.
14
     *
15
     * @param \Closure|string $callback
16
     *
17
     * @return void
18
     */
19
    abstract public static function deleted($callback);
20
21
    /**
22
     * Define a polymorphic one-to-many relationship.
23
     *
24
     * @param string $related
25
     * @param string $name
26
     * @param string $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
     * @param string $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
28
     * @param string $localKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $localKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
29
     *
30
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
31
     */
32
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
33
34
    /**
35
     * Boot the addressable trait for the model.
36
     *
37
     * @return void
38
     */
39
    public static function bootAddressable()
40
    {
41
        static::deleted(function (self $model) {
42
            $model->addresses()->delete();
43
        });
44
    }
45
46
    /**
47
     * Get all attached addresses to the model.
48
     *
49
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
50
     */
51
    public function addresses(): MorphMany
52
    {
53
        return $this->morphMany(config('rinvex.addresses.models.address'), 'addressable');
54
    }
55
56
    /**
57
     * Find addressables by distance.
58
     *
59
     * @param string $distance
60
     * @param string $unit
61
     * @param string $latitude
62
     * @param string $longitude
63
     *
64
     * @return \Illuminate\Support\Collection
65
     */
66
    public static function findByDistance($distance, $unit, $latitude, $longitude): Collection
67
    {
68
        $addressModel = config('rinvex.addresses.models.address');
69
        $records = (new $addressModel())->within($distance, $unit, $latitude, $longitude)->get();
70
71
        $results = [];
72
        foreach ($records as $record) {
73
            $results[] = $record->addressable;
74
        }
75
76
        return new Collection($results);
77
    }
78
}
79