Passed
Push — shop ( c2aba6...5180ff )
by Fèvre
04:54
created

ShopItem::users()   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
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\CountCache\Countable;
5
use Eloquence\Behaviours\Sluggable;
6
use Illuminate\Support\Facades\Auth;
7
use Spatie\Image\Exceptions\InvalidManipulation;
8
use Spatie\MediaLibrary\HasMedia;
9
use Spatie\MediaLibrary\InteractsWithMedia;
10
use Spatie\MediaLibrary\MediaCollections\Models\Media;
11
use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
12
use Xetaravel\Models\Presenters\ShopItemPresenter;
13
14
class ShopItem extends Model implements HasMedia
15
{
16
    use Countable,
0 ignored issues
show
introduced by
The trait Spatie\MediaLibrary\InteractsWithMedia requires some properties which are not provided by Xetaravel\Models\ShopItem: $fallbackPath, $mediaConversionRegistrations, $forceDeleting, $fallbackUrl, $media, $collection_name
Loading history...
17
        Sluggable,
18
        ShopItemPresenter,
19
        HasMentionsTrait,
20
        InteractsWithMedia;
21
22
    /**
23
     * The attributes that are mass assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = [
28
        'title',
29
        'user_id',
30
        'category_id',
31
        'content',
32
        'price',
33
        'discount',
34
        'quantity',
35
        'is_display',
36
        'start_at',
37
        'end_at'
38
    ];
39
40
    /**
41
     * The accessors to append to the model's array form.
42
     *
43
     * @var array
44
     */
45
    protected $appends = [
46
        'item_url',
47
48
        // Media Model
49
        'item_icon'
50
    ];
51
52
    /**
53
     * The attributes that should be mutated to dates.
54
     *
55
     * @var array
56
     */
57
    protected $dates = [
58
        'start_at',
59
        'end_at'
60
    ];
61
62
    /**
63
     * The "booting" method of the model.
64
     *
65
     * @return void
66
     */
67
    protected static function boot(): void
68
    {
69
        parent::boot();
70
71
        // Generated the slug before updating.
72
        static::updating(function ($model) {
73
            $model->generateSlug();
74
        });
75
76
        // Set the user id to the new item before saving it.
77
        static::creating(function ($model) {
78
            $model->user_id = Auth::id();
79
        });
80
    }
81
82
    /**
83
     * Return the field to slug.
84
     *
85
     * @return string
86
     */
87
    public function slugStrategy(): string
88
    {
89
        return 'title';
90
    }
91
92
    /**
93
     * Return the count cache configuration.
94
     *
95
     * @return array
96
     */
97
    public function countCaches(): array
98
    {
99
        return [
100
            User::class,
101
            ShopCategory::class
102
        ];
103
    }
104
105
    /**
106
     * Register the related to the Model.
107
     *
108
     * @param Media|null $media
109
     * @return void
110
     * @throws InvalidManipulation
111
     */
112
    public function registerMediaConversions(Media $media = null): void
113
    {
114
        $this->addMediaConversion('item.icon')
115
                ->width(100)
116
                ->height(100)
117
                ->keepOriginalImageFormat();
118
119
        $this->addMediaConversion('original')
120
                ->keepOriginalImageFormat();
121
    }
122
123
    /**
124
     * Get the category that owns the item.
125
     *
126
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
127
     */
128
    public function shopCategory()
129
    {
130
        return $this->belongsTo(ShopCategory::class);
131
    }
132
133
    /**
134
     * Get the users that owns the items.
135
     *
136
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTomany
137
     */
138
    public function users()
139
    {
140
        return $this->belongsToMany(User::class);
141
    }
142
143
    /**
144
     * Check if the given user has unlocked this item.
145
     *
146
     * @param User $user The user to check.
147
     *
148
     * @return bool
149
     */
150
    public function hasUser(User $user): bool
151
    {
152
        return $this->users()
153
            ->where('user_id', $user->getKey())
154
            ->exists();
155
    }
156
}
157