Completed
Pull Request — master (#83)
by Sebastian
03:53
created

Article::getFirstChildAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Models\Enums\SpecialArticle;
6
use App\Models\Presenters\ArticlePresenter;
7
use Cache;
8
use Exception;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
use Illuminate\Support\Collection;
12
use Spatie\Blender\Model\Model;
13
use Spatie\Blender\Model\Traits\HasSlug;
14
use Spatie\EloquentSortable\Sortable;
15
use Spatie\EloquentSortable\SortableTrait;
16
17
/**
18
 * @property \App\Models\Article $parent
19
 * @property \Illuminate\Support\Collection $children
20
 * @property \App\Models\Article $firstChild
21
 * @property \Illuminate\Support\Collection $siblings
22
 */
23
class Article extends Model implements Sortable
24
{
25
    use ArticlePresenter, HasSlug, SortableTrait;
26
27
    protected $with = ['media'];
28
29
    public $mediaLibraryCollections = ['images', 'downloads'];
30
    public $translatable = ['name', 'text', 'url', 'seo_values'];
31
32
    public function registerMediaConversions()
33
    {
34
        parent::registerMediaConversions();
35
36
        $this->addMediaConversion('thumb')
37
            ->setWidth(368)
38
            ->setHeight(232)
39
            ->performOnCollections('images');
40
    }
41
42
    public function isSpecialArticle(): bool
43
    {
44
        return ! empty($this->technical_name);
45
    }
46
47
    public function children(): HasMany
48
    {
49
        return $this->hasMany(self::class, 'parent_id')->orderBy('order_column');
50
    }
51
52
    public function hasChildren(): bool
53
    {
54
        return count($this->children);
55
    }
56
57
    public function getFirstChildAttribute(): Article
58
    {
59
        if (! $this->hasChildren()) {
60
            throw new Exception("Article `{$this->id}` doesn't have any children.");
61
        }
62
63
        return $this->children->sortBy('order_column')->first();
64
    }
65
66
    public function getSiblingsAttribute(): Collection
67
    {
68
        return Article::where('parent_id', $this->parent_id)
69
            ->orderBy('order_column')
70
            ->get();
71
    }
72
73
    public function parent(): BelongsTo
74
    {
75
        return $this->belongsTo(self::class, 'parent_id');
76
    }
77
78
    public function hasParent(): bool
79
    {
80
        return ! is_null($this->parent);
81
    }
82
83
    public function getFullUrlAttribute(): string
84
    {
85
        $localeSegment = '';
86
87
        if (locales()->count() > 1) {
88
            $localeSegment = '/'.locale();
89
        }
90
91
        if ($this->technical_name === SpecialArticle::HOME) {
92
            return $localeSegment;
93
        }
94
95
        $parentUrl = $this->hasParent() ? $this->parent->url.'/' : '';
96
97
        return "{$localeSegment}/{$parentUrl}{$this->url}";
98
    }
99
}
100