Passed
Push — master ( 6bcc6d...405e10 )
by F
03:06
created

HasAddresses::assignAddress()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 25
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 40
rs 9.2088
1
<?php
2
3
namespace PWWeb\Localisation\Traits;
4
5
/*
6
 * PWWeb\Localisation\Traits HasAddresses
7
 *
8
 * HasAddresses trait for use with other models.
9
 *
10
 * @package   PWWeb\Localisation
11
 * @author    Frank Pillukeit <[email protected]>
12
 * @copyright 2020 pw-websolutions.com
13
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
14
 */
15
16
use Illuminate\Database\Eloquent\Relations\MorphToMany;
17
use PWWeb\Localisation\LocalisationRegistrar;
18
use PWWeb\Localisation\Models\Address;
19
20
trait HasAddresses
21
{
22
    /**
23
     * Address class.
24
     *
25
     * @var string
26
     */
27
    private $addressClass;
28
29
    public function getAddressClass()
30
    {
31
        if (! isset($this->addressClass)) {
32
            $this->addressClass = app(LocalisationRegistrar::class)->getAddressClass();
0 ignored issues
show
Documentation Bug introduced by
It seems like app(PWWeb\Localisation\L...ass)->getAddressClass() of type PWWeb\Localisation\Contracts\Address is incompatible with the declared type string of property $addressClass.

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...
33
        }
34
35
        return $this->addressClass;
36
    }
37
38
    /**
39
     * A model may have multiple addresses.
40
     */
41
    public function addresses(): MorphToMany
42
    {
43
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

43
        return $this->/** @scrutinizer ignore-call */ morphToMany(
Loading history...
44
            config('localisation.models.address'),
45
            'model',
46
            config('localisation.table_names.model_has_address'),
47
            config('localisation.column_names.model_morph_key'),
48
            'address_id'
49
        );
50
    }
51
52
    /**
53
     * Assign the given address(es) to the model.
54
     *
55
     * @param array|string|\PWWeb\Localisation\Contracts\Address ...$addresses
56
     *
57
     * @return $this
58
     */
59
    public function assignAddress(...$addresses)
60
    {
61
        $addresses = collect($addresses)
62
            ->flatten()
63
            ->map(function ($address) {
64
                if (empty($address)) {
65
                    return false;
66
                }
67
68
                return $this->getStoredAddress($address);
69
            })
70
            ->filter(function ($address) {
71
                return $address instanceof Address;
72
            })
73
            ->map->id
74
            ->all();
75
76
        $model = $this->getModel();
0 ignored issues
show
Bug introduced by
It seems like getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        $model = $this->getModel();
Loading history...
77
78
        if ($model->exists) {
79
            $this->addresses()->sync($addresses, false);
80
            $model->load('addresses');
81
        } else {
82
            $class = \get_class($model);
83
84
            $class::saved(
85
                function ($object) use ($addresses, $model) {
86
                    static $modelLastFiredOn;
87
                    if (null !== $modelLastFiredOn && $modelLastFiredOn === $model) {
88
                        return;
89
                    }
90
                    $object->addresses()->sync($addresses, false);
91
                    $object->load('addresses');
92
                    $modelLastFiredOn = $object;
93
                });
94
        }
95
96
        //$this->forgetCachedAddresses();
97
98
        return $this;
99
    }
100
101
    protected function getStoredAddress($address): Address
102
    {
103
        $addressClass = $this->getAddressClass();
104
105
        if (is_numeric($address)) {
106
            return $addressClass->findById($address, $this->getDefaultGuardName());
0 ignored issues
show
Bug introduced by
It seems like getDefaultGuardName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

106
            return $addressClass->findById($address, $this->/** @scrutinizer ignore-call */ getDefaultGuardName());
Loading history...
107
        }
108
109
        if (is_string($address)) {
110
            return $addressClass->findByName($address, $this->getDefaultGuardName());
111
        }
112
113
        return $address;
114
    }
115
}
116