Passed
Push — 5.0.0 ( 49e1c0...87aae2 )
by Fèvre
06:22
created

ArticlePresenter::getArticleUrlAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Models\Presenters;
6
7
use Illuminate\Database\Eloquent\Casts\Attribute;
8
9
trait ArticlePresenter
10
{
11
    /**
12
     * The default banner used when there is no banner for the article.
13
     *
14
     * @var string
15
     */
16
    protected string $defaultBanner = '/images/articles/default_banner.jpg';
17
18
    /**
19
     * Get the is_display field.
20
     *
21
     * @return Attribute
22
     */
23
    protected function isDisplay(): Attribute
24
    {
25
        return Attribute::make(
26
            get: fn ($value) => !(is_null($this->published_at) || $this->published_at > now())
27
        );
28
    }
29
30
    /**
31
     * Parse a media and return it if isset or return the default banner.
32
     *
33
     * @param string $type The type of the media to get.
34
     *
35
     * @return string
36
     */
37
    protected function parseMedia(string $type): string
38
    {
39
        if (isset($this->getMedia('article')[0])) {
0 ignored issues
show
Bug introduced by
It seems like getMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

39
        if (isset($this->/** @scrutinizer ignore-call */ getMedia('article')[0])) {
Loading history...
40
            return $this->getMedia('article')[0]->getUrl($type);
41
        }
42
43
        return $this->defaultBanner;
44
    }
45
46
    /**
47
     * Get the show url.
48
     *
49
     * @return string
50
     */
51
    public function getShowUrlAttribute(): string
52
    {
53
        if (!isset($this->slug) || $this->getKey() === null) {
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
        if (!isset($this->slug) || $this->/** @scrutinizer ignore-call */ getKey() === null) {
Loading history...
54
            return '';
55
        }
56
57
        return route('blog.article.show', ['slug' => $this->slug, 'id' => $this->getKey()]);
58
    }
59
60
    /**
61
     * Get the small avatar.
62
     *
63
     * @return string
64
     */
65
    public function getArticleBannerAttribute(): string
66
    {
67
        return $this->parseMedia('article.banner');
68
    }
69
}
70