DiscussCategory::casts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
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
        'show_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
     * Pluck the categories by the given fields and the locked state.
58
     *
59
     * @param string $value
60
     * @param string|null $column
61
     *
62
     * @return Collection
63
     */
64
    public static function pluckLocked(string $value, string $column = null): Collection
65
    {
66
        if (Auth::user() && Auth::user()->hasPermissionTo('manage discuss conversation')) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() 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

66
        if (Auth::user() && Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('manage discuss conversation')) {
Loading history...
67
            return self::pluck($value, $column);
68
        }
69
70
        return self::where('is_locked', false)->pluck($value, $column);
71
    }
72
73
    /**
74
     * Return the field to slug.
75
     *
76
     * @return string
77
     */
78
    public function slugStrategy(): string
79
    {
80
        return 'title';
81
    }
82
83
    /**
84
     * Get the conversations for the category.
85
     *
86
     * @return HasMany
87
     */
88
    public function conversations(): HasMany
89
    {
90
        return $this->hasMany(DiscussConversation::class, 'category_id', 'id');
91
    }
92
93
    /**
94
     * Get the last conversation of the category.
95
     *
96
     * @return HasOne
97
     */
98
    public function lastConversation(): HasOne
99
    {
100
        return $this->hasOne(DiscussConversation::class, 'id', 'last_conversation_id');
101
    }
102
}
103