HasAddresses::getStoredAddress()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PWWEB\Localisation\Traits HasAddresses.
5
 *
6
 * HasAddresses trait for use with other models.
7
 *
8
 * @author    Frank Pillukeit <[email protected]>
9
 * @copyright 2020 pw-websolutions.com
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
11
 */
12
13
namespace PWWEB\Localisation\Traits;
14
15
use Illuminate\Database\Eloquent\Relations\MorphToMany;
16
use PWWEB\Localisation\LocalisationRegistrar;
17
use PWWEB\Localisation\Models\Address;
18
19
trait HasAddresses
20
{
21
    /**
22
     * Address class.
23
     *
24
     * @var string
25
     */
26
    private $addressClass;
27
28
    /**
29
     * Retrieve and return the address class to be used.
30
     *
31
     * @return string
32
     */
33
    public function getAddressClass(): string
34
    {
35
        if (false === isset($this->addressClass)) {
36
            $this->addressClass = app(LocalisationRegistrar::class)->getAddressModel();
0 ignored issues
show
Documentation Bug introduced by
It seems like app(PWWEB\Localisation\L...ass)->getAddressModel() 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...
37
        }
38
39
        return $this->addressClass;
40
    }
41
42
    /**
43
     * A model may have multiple addresses.
44
     *
45
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
46
     */
47
    public function addresses(): MorphToMany
48
    {
49
        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

49
        return $this->/** @scrutinizer ignore-call */ morphToMany(
Loading history...
50
            config('pwweb.localisation.models.address'),
51
            'model',
52
            config('pwweb.localisation.table_names.model_has_address'),
53
            config('pwweb.localisation.column_names.model_morph_key'),
54
            'address_id'
55
        );
56
    }
57
58
    /**
59
     * Assign the given address(es) to the model.
60
     *
61
     * @param array|string|\PWWEB\Localisation\Contracts\Address ...$addresses One or multiple addresses to be added to the user.
62
     *
63
     * @return mixed
64
     */
65
    public function assignAddress(...$addresses)
66
    {
67
        $addresses = collect($addresses)
68
            ->flatten()
69
            ->map(
70
                function ($address) {
71
                    if (true === empty($address)) {
72
                        return false;
73
                    }
74
75
                    return $this->getStoredAddress($address);
76
                }
77
            )
78
            ->filter(
79
                function ($address) {
80
                    return $address instanceof Address;
81
                }
82
            )
83
            ->map->id
84
            ->all();
85
86
        $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

86
        /** @scrutinizer ignore-call */ 
87
        $model = $this->getModel();
Loading history...
87
88
        if (true === $model->exists) {
89
            $this->addresses()->sync($addresses, false);
90
            $model->load('addresses');
91
        } else {
92
            $class = \get_class($model);
93
94
            $class::saved(
95
                function ($object) use ($addresses, $model) {
96
                    static $modelLastFiredOn;
97
                    if (null !== $modelLastFiredOn && $modelLastFiredOn === $model) {
98
                        return;
99
                    }
100
                    $object->addresses()->sync($addresses, false);
101
                    $object->load('addresses');
102
                    $modelLastFiredOn = $object;
103
                }
104
            );
105
        }
106
107
        // TEMP: $this->forgetCachedAddresses();
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get a stored address from the cache.
114
     *
115
     * @param int|string $address Address to be retrieved from cache
116
     *
117
     * @return \PWWEB\Localisation\Contracts\Address
118
     */
119
    protected function getStoredAddress($address): Address
120
    {
121
        $addressClass = $this->getAddressClass();
122
123
        if (true === is_numeric($address)) {
124
            return $addressClass->findById($address);
125
        }
126
127
        if (true === is_string($address)) {
0 ignored issues
show
introduced by
The condition true === is_string($address) is always true.
Loading history...
128
            return $addressClass->findByName($address);
129
        }
130
131
        return $address;
132
    }
133
}
134