Completed
Pull Request — master (#81)
by Sebastian
20:20 queued 12:24
created

Article::children()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
15
class Article extends Model
16
{
17
    use HasSlug, ArticlePresenter;
18
19
    protected $with = ['media'];
20
21
    public $mediaLibraryCollections = ['images', 'downloads'];
22
    public $translatable = ['name', 'text', 'url', 'seo_values'];
23
24
    public function registerMediaConversions()
25
    {
26
        parent::registerMediaConversions();
27
28
        $this->addMediaConversion('thumb')
29
            ->setWidth(368)
30
            ->setHeight(232)
31
            ->performOnCollections('images');
32
    }
33
34
    public static function findByTechnicalName(string $technicalName): Article
35
    {
36
        return Cache::rememberForever(
37
            "article.findByTechnicalName.{$technicalName}",
38
            function () use ($technicalName) : Article {
39
                $article = static::where('technical_name', $technicalName)->first();
40
41
                if ($article === null) {
42
                    throw new Exception("Article `{$technicalName}` not found");
43
                }
44
45
                return $article;
46
            }
47
        );
48
    }
49
50
    public static function getWithTechnicalNameLike(string $technicalName): Collection
51
    {
52
        return Cache::rememberForever(
53
            "article.getWithTechnicalNameLike.{$technicalName}",
54
            function () use ($technicalName) : Collection {
55
                return static::where('technical_name', 'like', "{$technicalName}.%")
56
                    ->orderBy('order_column')
57
                    ->get();
58
            }
59
        );
60
    }
61
62
    public function isDeletable(): bool
63
    {
64
        return ! (bool) $this->technical_name;
65
    }
66
67
    public function children(): HasMany
68
    {
69
        return $this->hasMany(self::class, 'parent_id')->orderBy('order_column');
70
    }
71
72
    public function hasChildren(): bool
73
    {
74
        return count($this->children);
75
    }
76
77
    public function parentArticle(): BelongsTo
78
    {
79
        return $this->belongsTo(self::class, 'parent_id');
80
    }
81
82
    public function hasParentArticle(): bool
83
    {
84
        return ! is_null($this->parentArticle);
85
    }
86
87
    public function getFullUrlAttribute(): string
88
    {
89
        $localeSegment = '';
90
91
        if (locales()->count() > 1) {
92
            $localeSegment = '/'.locale();
93
        }
94
95
        if ($this->technical_name === SpecialArticle::HOME) {
96
            return $localeSegment;
97
        }
98
99
        $parentUrl = $this->hasParentArticle() ? $this->parentArticle->url.'/' : '';
100
101
        return "{$localeSegment}/{$parentUrl}{$this->url}";
102
    }
103
}
104