BlogArticle   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 102
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A casts() 0 5 1
A registerMediaConversions() 0 6 1
A slugStrategy() 0 3 1
A category() 0 4 1
A comments() 0 3 1
A user() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Models;
6
7
use Eloquence\Behaviours\CountCache\CountedBy;
8
use Eloquence\Behaviours\CountCache\HasCounts;
9
use Eloquence\Behaviours\HasSlugs;
10
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Spatie\Image\Enums\Fit;
14
use Spatie\MediaLibrary\HasMedia;
15
use Spatie\MediaLibrary\InteractsWithMedia;
16
use Spatie\MediaLibrary\MediaCollections\Models\Media;
17
use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
18
use Xetaravel\Models\Presenters\BlogArticlePresenter;
19
use Xetaravel\Observers\BlogArticleObserver;
20
21
#[ObservedBy([BlogArticleObserver::class])]
22
class BlogArticle extends Model implements HasMedia
23
{
24
    use BlogArticlePresenter;
0 ignored issues
show
introduced by
The trait Xetaravel\Models\Presenters\BlogArticlePresenter requires some properties which are not provided by Xetaravel\Models\BlogArticle: $published_at, $slug
Loading history...
25
    use HasCounts;
26
    use HasMentionsTrait;
27
    use HasSlugs;
28
    use InteractsWithMedia;
0 ignored issues
show
introduced by
The trait Spatie\MediaLibrary\InteractsWithMedia requires some properties which are not provided by Xetaravel\Models\BlogArticle: $fallbackPaths, $fallbackUrls, $mediaConversionRegistrations, $forceDeleting, $media, $collection_name
Loading history...
29
30
    /**
31
     * The attributes that are mass assignable.
32
     *
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'title',
37
        'slug',
38
        'user_id',
39
        'blog_category_id',
40
        'published_at',
41
        'content'
42
    ];
43
44
    /**
45
     * The accessors to append to the model's array form.
46
     *
47
     * @var array
48
     */
49
    protected $appends = [
50
        'show_url',
51
        'is_display',
52
53
        // Media Model
54
        'article_banner'
55
    ];
56
57
    /**
58
     * The attributes that should be cast.
59
     */
60
    protected function casts(): array
61
    {
62
        return [
63
            'published_at' => 'datetime',
64
            'is_display' => 'boolean',
65
        ];
66
    }
67
68
    /**
69
     * Return the field to slug.
70
     *
71
     * @return string
72
     */
73
    public function slugStrategy(): string
74
    {
75
        return 'title';
76
    }
77
78
    /**
79
     * Register the related to the Model.
80
     *
81
     * @param Media|null $media
82
     *
83
     * @return void
84
     */
85
    public function registerMediaConversions(Media $media = null): void
86
    {
87
        $this->addMediaConversion('article.banner')
88
            ->fit(Fit::Contain, 870, 350)
89
            ->keepOriginalImageFormat()
90
            ->nonQueued();
91
    }
92
93
    /**
94
     * Get the category that owns the article.
95
     *
96
     * @return BelongsTo
97
     */
98
    #[CountedBy(as: 'blog_article_count')]
99
    public function category(): BelongsTo
100
    {
101
        return $this->belongsTo(BlogCategory::class, 'blog_category_id');
102
    }
103
104
    /**
105
     * Get the user that owns the article.
106
     *
107
     * @return BelongsTo
108
     */
109
    #[CountedBy(as: 'blog_article_count')]
110
    public function user(): BelongsTo
111
    {
112
        return $this->belongsTo(User::class)->withTrashed();
113
    }
114
115
    /**
116
     * Get the comments for the article.
117
     *
118
     * @return HasMany
119
     */
120
    public function comments(): HasMany
121
    {
122
        return $this->hasMany(BlogComment::class);
123
    }
124
}
125