Issues (19)

src/Models/Address.php (6 issues)

1
<?php
2
3
namespace Chuckcms\Addresses\Models;
4
5
use Chuckcms\Addresses\Contracts\Address as AddressContract;
6
use Chuckcms\Addresses\Exceptions\AddressDoesNotExist;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
11
class Address extends Model implements AddressContract
12
{
13
    use SoftDeletes;
14
15
    protected $guarded = ['id'];
16
17
    /**
18
     * The attributes that are fillable on this model.
19
     *
20
     * @var array
21
     */
22
    protected $fillable = ['addressable_id', 'addressable_type'];
23
24
    /**
25
     * The default rules that the model will validate against.
26
     *
27
     * @var array
28
     */
29
    protected $rules = [];
30
31
    public function __construct(array $attributes = [])
32
    {
33
        $this->setTable(config('addresses.table_names.addresses'));
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

33
        $this->setTable(/** @scrutinizer ignore-call */ config('addresses.table_names.addresses'));
Loading history...
34
        $this->mergeFillables();
35
36
        parent::__construct($attributes);
37
    }
38
39
    /**
40
     * Merge fillable fields.
41
     *
42
     * @return void.
0 ignored issues
show
Documentation Bug introduced by
The doc comment void. at position 0 could not be parsed: Unknown type name 'void.' at position 0 in void..
Loading history...
43
     */
44
    private function mergeFillables()
45
    {
46
        $fillable = $this->fillable;
47
        $columns = array_keys(config('addresses.fields.addresses'));
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

47
        $columns = array_keys(/** @scrutinizer ignore-call */ config('addresses.fields.addresses'));
Loading history...
48
49
        $this->fillable(array_merge($fillable, $columns));
50
    }
51
52
    /**
53
     * Get the related model.
54
     *
55
     * @return MorphTo
56
     */
57
    public function addressable(): MorphTo
0 ignored issues
show
The type Chuckcms\Addresses\Models\MorphTo 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...
58
    {
59
        return $this->morphTo();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->morphTo() returns the type Illuminate\Database\Eloquent\Relations\MorphTo which is incompatible with the type-hinted return Chuckcms\Addresses\Models\MorphTo.
Loading history...
60
    }
61
62
    /**
63
     * Get the validation rules.
64
     *
65
     * @return array
66
     */
67
    public static function getValidationRules(): array
68
    {
69
        $rules = config('addresses.fields.addresses');
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

69
        $rules = /** @scrutinizer ignore-call */ config('addresses.fields.addresses');
Loading history...
70
71
        return $rules;
72
    }
73
74
    /**
75
     * Scope public addresses.
76
     *
77
     * @param \Illuminate\Database\Eloquent\Builder $builder
78
     *
79
     * @return \Illuminate\Database\Eloquent\Builder
80
     */
81
    public function scopeIsPublic(Builder $builder): Builder
82
    {
83
        return $builder->where('is_public', true);
84
    }
85
86
    /**
87
     * Scope primary addresses.
88
     *
89
     * @param \Illuminate\Database\Eloquent\Builder $builder
90
     *
91
     * @return \Illuminate\Database\Eloquent\Builder
92
     */
93
    public function scopeIsPrimary(Builder $builder): Builder
94
    {
95
        return $builder->where('is_primary', true);
96
    }
97
98
    /**
99
     * Scope billing addresses.
100
     *
101
     * @param \Illuminate\Database\Eloquent\Builder $builder
102
     *
103
     * @return \Illuminate\Database\Eloquent\Builder
104
     */
105
    public function scopeIsBilling(Builder $builder): Builder
106
    {
107
        return $builder->where('is_billing', true);
108
    }
109
110
    /**
111
     * Scope shipping addresses.
112
     *
113
     * @param \Illuminate\Database\Eloquent\Builder $builder
114
     *
115
     * @return \Illuminate\Database\Eloquent\Builder
116
     */
117
    public function scopeIsShipping(Builder $builder): Builder
118
    {
119
        return $builder->where('is_shipping', true);
120
    }
121
122
    /**
123
     * Scope addresses by the given country.
124
     *
125
     * @param \Illuminate\Database\Eloquent\Builder $builder
126
     * @param string                                $countryCode
127
     *
128
     * @return \Illuminate\Database\Eloquent\Builder
129
     */
130
    public function scopeInCountry(Builder $builder, string $country): Builder
131
    {
132
        return $builder->where('country', $country);
133
    }
134
135
    public static function findById(int $id): AddressContract
136
    {
137
        $address = static::where('id', $id)->first();
138
139
        if (!$address) {
140
            throw AddressDoesNotExist::withId($id);
141
        }
142
143
        return $address;
144
    }
145
}
146