Completed
Push — develop ( 347a45...1a5bb2 )
by Abdelrahman
01:12
created

Attribute::boot()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Attribute::setGroupAttribute() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Attributes\Models;
6
7
use Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Spatie\EloquentSortable\Sortable;
10
use Illuminate\Database\Eloquent\Model;
11
use Rinvex\Cacheable\CacheableEloquent;
12
use Illuminate\Database\Eloquent\Builder;
13
use Rinvex\Support\Traits\HasTranslations;
14
use Rinvex\Support\Traits\ValidatingTrait;
15
use Spatie\EloquentSortable\SortableTrait;
16
use Rinvex\Attributes\Contracts\AttributeContract;
17
use Illuminate\Database\Eloquent\Relations\HasMany;
18
19
/**
20
 * Rinvex\Attributes\Models\Attribute.
21
 *
22
 * @property int                                                                               $id
23
 * @property string                                                                            $slug
24
 * @property array                                                                             $name
25
 * @property array                                                                             $description
26
 * @property int                                                                               $sort_order
27
 * @property string                                                                            $group
28
 * @property string                                                                            $type
29
 * @property bool                                                                              $is_required
30
 * @property bool                                                                              $is_collection
31
 * @property string                                                                            $default
32
 * @property \Carbon\Carbon|null                                                               $created_at
33
 * @property \Carbon\Carbon|null                                                               $updated_at
34
 * @property array                                                                             $entities
35
 * @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values
36
 *
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute ordered($direction = 'asc')
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereCreatedAt($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereDefault($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereDescription($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereGroup($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereId($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereIsCollection($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereIsRequired($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereName($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereSlug($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereSortOrder($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereType($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute whereUpdatedAt($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Attribute withGroup($group)
51
 * @mixin \Eloquent
52
 */
53
class Attribute extends Model implements AttributeContract, Sortable
54
{
55
    use HasSlug;
56
    use SortableTrait;
57
    use HasTranslations;
58
    use ValidatingTrait;
59
    use CacheableEloquent;
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected $fillable = [
65
        'name',
66
        'slug',
67
        'description',
68
        'sort_order',
69
        'group',
70
        'type',
71
        'entities',
72
        'is_required',
73
        'is_collection',
74
        'default',
75
    ];
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected $casts = [
81
        'slug' => 'string',
82
        'sort_order' => 'integer',
83
        'group' => 'string',
84
        'type' => 'string',
85
        'is_required' => 'boolean',
86
        'is_collection' => 'boolean',
87
        'default' => 'string',
88
    ];
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected $observables = [
94
        'validating',
95
        'validated',
96
    ];
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public $translatable = [
102
        'name',
103
        'description',
104
    ];
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public $sortable = [
110
        'order_column_name' => 'sort_order',
111
    ];
112
113
    /**
114
     * The default rules that the model will validate against.
115
     *
116
     * @var array
117
     */
118
    protected $rules = [];
119
120
    /**
121
     * Whether the model should throw a
122
     * ValidationException if it fails validation.
123
     *
124
     * @var bool
125
     */
126
    protected $throwValidationExceptions = true;
127
128
    /**
129
     * Create a new Eloquent model instance.
130
     *
131
     * @param array $attributes
132
     */
133
    public function __construct(array $attributes = [])
134
    {
135
        parent::__construct($attributes);
136
137
        $this->setTable(config('rinvex.attributes.tables.attributes'));
138
        $this->setRules([
139
            'name' => 'required|string|max:150',
140
            'description' => 'nullable|string|max:10000',
141
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.attributes.tables.attributes').',slug',
142
            'sort_order' => 'nullable|integer|max:10000000',
143
            'group' => 'nullable|string|max:150',
144
            'type' => 'required|string|max:150',
145
            'is_required' => 'sometimes|boolean',
146
            'is_collection' => 'sometimes|boolean',
147
            'default' => 'nullable|string|max:10000',
148
        ]);
149
    }
150
151
    /**
152
     * Enforce clean groups.
153
     *
154
     * @param string $value
155
     *
156
     * @return void
157
     */
158
    public function setGroupAttribute($value)
159
    {
160
        $this->attributes['group'] = str_slug($value);
161
    }
162
163
    /**
164
     * Access entities relation and retrieve entity types as an array,
165
     * Accessors/Mutators preceeds relation value when called dynamically.
166
     *
167
     * @return array
168
     */
169
    public function getEntitiesAttribute(): array
170
    {
171
        return $this->entities()->pluck('entity_type')->toArray();
172
    }
173
174
    /**
175
     * Set the attribute attached entities.
176
     *
177
     * @param \Illuminate\Support\Collection|array $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
     *
179
     * @return void
180
     */
181
    public function setEntitiesAttribute($entities)
182
    {
183
        static::saved(function ($model) use ($entities) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
            $this->entities()->delete();
185
            ! $entities || $this->entities()->createMany(array_map(function ($entity) {
186
                return ['entity_type' => $entity];
187
            }, $entities));
188
        });
189
    }
190
191
    /**
192
     * Get the options for generating the slug.
193
     *
194
     * @return \Spatie\Sluggable\SlugOptions
195
     */
196
    public function getSlugOptions(): SlugOptions
197
    {
198
        return SlugOptions::create()
199
                          ->doNotGenerateSlugsOnUpdate()
200
                          ->generateSlugsFrom('name')
201
                          ->saveSlugsTo('slug');
202
    }
203
204
    /**
205
     * Scope attributes by given group.
206
     *
207
     * @param \Illuminate\Database\Eloquent\Builder $builder
208
     * @param string                                $group
209
     *
210
     * @return \Illuminate\Database\Eloquent\Builder
211
     */
212
    public function scopeWithGroup(Builder $builder, string $group): Builder
213
    {
214
        return $group ? $builder->where('group', $group) : $builder;
215
    }
216
217
    /**
218
     * Get the entities attached to this attribute.
219
     *
220
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
221
     */
222
    public function entities(): HasMany
223
    {
224
        return $this->hasMany(config('rinvex.attributes.models.attribute_entity'), 'attribute_id', 'id');
225
    }
226
227
    /**
228
     * Get the entities attached to this attribute.
229
     *
230
     * @param string $value
231
     *
232
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
233
     */
234
    public function values(string $value): HasMany
235
    {
236
        return $this->hasMany($value, 'attribute_id', 'id');
237
    }
238
}
239