Passed
Push — master ( 43634e...e872b0 )
by F
03:23
created

Address::systemModelHasAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PWWEB\Localisation\Models;
4
5
use Eloquent as Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
8
/**
9
 * PWWEB\Localisation\Models\Address Model.
10
 *
11
 * Standard Address Model.
12
 *
13
 * @package   pwweb/localisation
14
 * @author    Frank Pillukeit <[email protected]>
15
 * @author    Richard Browne <[email protected]
16
 * @copyright 2020 pw-websolutions.com
17
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
18
 * @property  \PWWEB\Localisation\Models\SystemLocalisationCountry country
19
 * @property  \PWWEB\Localisation\Models\SystemAddressType type
20
 * @property  \PWWEB\Localisation\Models\SystemModelHasAddress systemModelHasAddress
21
 * @property  integer country_id
22
 * @property  integer type_id
23
 * @property  string street
24
 * @property  string street2
25
 * @property  string city
26
 * @property  string state
27
 * @property  string postcode
28
 * @property  number lat
29
 * @property  number lng
30
 * @property  boolean primary
31
 */
32
33
class Address extends Model
34
{
35
    use SoftDeletes;
36
37
    const CREATED_AT = 'created_at';
38
    const UPDATED_AT = 'updated_at';
39
40
    /**
41
     * The attributes that should be casted to Carbon dates.
42
     *
43
     * @var string[]
44
     */
45
    protected $dates = [
46
        'deleted_at'
47
    ];
48
49
    /**
50
     * The attributes that can be filled.
51
     *
52
     * @var string[]
53
     */
54
    public $fillable = [
55
        'country_id',
56
        'type_id',
57
        'street',
58
        'street2',
59
        'city',
60
        'state',
61
        'postcode',
62
        'lat',
63
        'lng',
64
        'primary'
65
    ];
66
67
    /**
68
     * The attributes that should be casted to native types.
69
     *
70
     * @var array
71
     */
72
    protected $casts = [
73
        'id' => 'integer',
74
        'country_id' => 'integer',
75
        'type_id' => 'integer',
76
        'street' => 'string',
77
        'street2' => 'string',
78
        'city' => 'string',
79
        'state' => 'string',
80
        'postcode' => 'string',
81
        'lat' => 'float',
82
        'lng' => 'float',
83
        'primary' => 'boolean'
84
    ];
85
86
    /**
87
     * Validation rules.
88
     *
89
     * @var array
90
     */
91
    public static $rules = [
92
        'country_id' => 'required',
93
        'type_id' => 'required',
94
        'primary' => 'required'
95
    ];
96
97
    /**
98
     * Constructor.
99
     *
100
     * @param array $attributes additional attributes for model initialisation
101
     *
102
     * @return void
103
     */
104
    public function __construct(array $attributes = [])
105
    {
106
        parent::__construct($attributes);
107
108
        $this->setTable(config('pwweb.localisation.table_names.addresses'));
109
    }
110
111
    /**
112
     * Accessor for linked Country model.
113
     *
114
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
115
     **/
116
    public function country()
117
    {
118
        return $this->belongsTo(\PWWEB\Localisation\Models\Country::class, 'country_id');
119
    }
120
121
    /**
122
     * Accessor for linked Address type model.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
125
     **/
126
    public function type()
127
    {
128
        return $this->belongsTo(\PWWEB\Localisation\Models\Address\Type::class, 'type_id');
129
    }
130
131
    /**
132
     * Find an address by its id.
133
     *
134
     * @param int $id ID to be used to retrieve the address
135
     *
136
     * @todo Refactor. This needs to go into the repository.
137
     *
138
     * @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist
139
     *
140
     * @return \PWWEB\Localisation\Contracts\Address
141
     */
142
    public static function findById(int $id): AddressContract
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Models\AddressContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
143
    {
144
        $address = static::getAddresses(['id' => $id])->first();
145
146
        if (null === $address) {
147
            throw AddressDoesNotExist::withId($id);
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Models\AddressDoesNotExist was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
148
        }
149
150
        return $address;
151
    }
152
153
    /**
154
     * Find an address by its type.
155
     *
156
     * @param string $type Address type to be used to retrieve the address
157
     *
158
     * @todo Refactor. This needs to go into the repository.
159
     *
160
     * @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist
161
     *
162
     * @return \PWWEB\Localisation\Contracts\Address
163
     */
164
    public static function findByType(string $type): AddressContract
165
    {
166
        $address = static::getAddresses(['type' => $type])->first();
167
168
        if (null === $address) {
169
            throw AddressDoesNotExist::withType($type);
170
        }
171
172
        return $address;
173
    }
174
175
    /**
176
     * Get the current cached addresses.
177
     *
178
     * @param array $params additional parameters for the database query
179
     *
180
     * @return Collection collection of addresses
181
     */
182
    protected static function getAddresses(array $params = []): Collection
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Models\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
    {
184
        return app(LocalisationRegistrar::class)
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Models\LocalisationRegistrar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
185
            ->setAddressClass(static::class)
0 ignored issues
show
Bug introduced by
The method setAddressClass() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

185
            ->/** @scrutinizer ignore-call */ setAddressClass(static::class)

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...
186
            ->getAddresses($params);
187
    }
188
}
189