Completed
Push — master ( f81fd9...e392a3 )
by Fèvre
21s queued 15s
created

ArticlePresenter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 47
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getArticleBannerAttribute() 0 3 1
A getArticleUrlAttribute() 0 7 2
A parseMedia() 0 7 2
1
<?php
2
namespace Xetaravel\Models\Presenters;
3
4
trait ArticlePresenter
5
{
6
    /**
7
     * The default banner used when there is no banner for the article.
8
     *
9
     * @var string
10
     */
11
    protected $defaultBanner = '/images/articles/default_banner.jpg';
12
13
    /**
14
     * Get the article url.
15
     *
16
     * @return string
17
     */
18
    public function getArticleUrlAttribute(): string
19
    {
20
        if (!isset($this->slug)) {
21
            return '';
22
        }
23
24
        return route('blog.article.show', ['slug' => $this->slug, 'id' => $this->getKey()]);
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

24
        return route('blog.article.show', ['slug' => $this->slug, 'id' => $this->/** @scrutinizer ignore-call */ getKey()]);
Loading history...
25
    }
26
27
    /**
28
     * Get the small avatar.
29
     *
30
     * @return string
31
     */
32
    public function getArticleBannerAttribute(): string
33
    {
34
        return $this->parseMedia('article.banner');
35
    }
36
37
    /**
38
     * Parse a media and return it if isset or return the default banner.
39
     *
40
     * @param string $type The type of the media to get.
41
     *
42
     * @return string
43
     */
44
    protected function parseMedia(string $type): string
45
    {
46
        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

46
        if (isset($this->/** @scrutinizer ignore-call */ getMedia('article')[0])) {
Loading history...
47
            return $this->getMedia('article')[0]->getUrl($type);
48
        }
49
50
        return $this->defaultBanner;
51
    }
52
}
53