Total Complexity | 6 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
9 | trait BlogArticlePresenter |
||
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 () => !(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])) { |
||
|
|||
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 Attribute |
||
50 | */ |
||
51 | protected function showUrl(): Attribute |
||
52 | { |
||
53 | return Attribute::make( |
||
54 | get: fn () => route('blog.article.show', ['slug' => $this->slug, 'id' => $this->getKey()]) |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get the article banner. |
||
60 | * |
||
61 | * @return Attribute |
||
62 | */ |
||
63 | public function articleBanner(): Attribute |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 |