Passed
Push — 5.0.0 ( f07a46...357374 )
by Fèvre
05:39
created

DiscussCategory::slugStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Models;
6
7
use Eloquence\Behaviours\HasSlugs;
8
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use Illuminate\Support\Collection;
12
use Illuminate\Support\Facades\Auth;
13
use Xetaravel\Models\Presenters\DiscussCategoryPresenter;
14
use Xetaravel\Observers\DiscussCategoryObserver;
15
16
#[ObservedBy([DiscussCategoryObserver::class])]
17
class DiscussCategory extends Model
18
{
19
    use DiscussCategoryPresenter;
20
    use HasSlugs;
21
22
    /**
23
     * The attributes that are mass assignable.
24
     *
25
     * @var array
26
     */
27
    protected $fillable = [
28
        'title',
29
        'slug',
30
        'color',
31
        'is_locked',
32
        'level',
33
        'icon',
34
        'description'
35
    ];
36
37
    /**
38
     * The accessors to append to the model's array form.
39
     *
40
     * @var array
41
     */
42
    protected $appends = [
43
        'category_url'
44
    ];
45
46
    /**
47
     * The attributes that should be cast.
48
     */
49
    protected function casts(): array
50
    {
51
        return [
52
            'is_locked' => 'boolean'
53
        ];
54
    }
55
56
    /**
57
     * Return the field to slug.
58
     *
59
     * @return string
60
     */
61
    public function slugStrategy(): string
62
    {
63
        return 'title';
64
    }
65
66
    /**
67
     * Get the conversations for the category.
68
     *
69
     * @return HasMany
70
     */
71
    public function conversations(): HasMany
72
    {
73
        return $this->hasMany(DiscussConversation::class, 'category_id', 'id');
74
    }
75
76
    /**
77
     * Get the last conversation of the category.
78
     *
79
     * @return HasOne
80
     */
81
    public function lastConversation(): HasOne
82
    {
83
        return $this->hasOne(DiscussConversation::class, 'id', 'last_conversation_id');
84
    }
85
86
    /**
87
     * Pluck the categories by the given fields and the locked state.
88
     *
89
     * @param string $value
90
     * @param string|null $column
91
     *
92
     * @return Collection
93
     */
94
    public static function pluckLocked(string $value, string $column = null): Collection
95
    {
96
        if (Auth::user() && Auth::user()->hasPermission('manage.discuss.conversations')) {
0 ignored issues
show
Bug introduced by
The method hasPermission() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

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

96
        if (Auth::user() && Auth::user()->/** @scrutinizer ignore-call */ hasPermission('manage.discuss.conversations')) {
Loading history...
97
            return self::pluck($value, $column);
98
        }
99
100
        return self::where('is_locked', false)->pluck($value, $column);
101
    }
102
}
103