Article::isSpecialArticle()   A
last analyzed

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 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Exception;
6
use Spatie\Blender\Model\Model;
7
use Illuminate\Support\Collection;
8
use App\Models\Enums\SpecialArticle;
9
use Spatie\EloquentSortable\Sortable;
10
use Spatie\Blender\Model\Traits\HasSlug;
11
use Spatie\EloquentSortable\SortableTrait;
12
use App\Models\Presenters\ArticlePresenter;
13
use Illuminate\Database\Eloquent\Relations\HasMany;
14
use Illuminate\Database\Eloquent\Relations\BelongsTo;
15
16
/**
17
 * @property \App\Models\Article $parent
18
 * @property \Illuminate\Support\Collection $children
19
 * @property \App\Models\Article $firstChild
20
 * @property \Illuminate\Support\Collection $siblings
21
 */
22
class Article extends Model implements Sortable
23
{
24
    use ArticlePresenter, HasSlug, SortableTrait;
25
26
    protected $with = ['media'];
27
28
    protected $mediaLibraryCollections = ['images', 'downloads'];
29
30
    public $translatable = ['name', 'text', 'slug', '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($specialArticleName = ''): bool
43
    {
44
        if ($specialArticleName === '') {
45
            return ! empty($this->technical_name);
0 ignored issues
show
Documentation introduced by
The property technical_name does not exist on object<App\Models\Article>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
        }
47
48
        return $this->technical_name === $specialArticleName;
0 ignored issues
show
Documentation introduced by
The property technical_name does not exist on object<App\Models\Article>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
    }
50
51
    public function children(): HasMany
52
    {
53
        return $this->hasMany(self::class, 'parent_id')->orderBy('order_column');
54
    }
55
56
    public function hasChildren(): bool
57
    {
58
        return count($this->children);
59
    }
60
61
    public function getFirstChildAttribute(): Article
62
    {
63
        if (! $this->hasChildren()) {
64
            throw new Exception("Article `{$this->id}` doesn't have any children.");
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Models\Article>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
        }
66
67
        return $this->children->sortBy('order_column')->first();
68
    }
69
70
    public function getSiblingsAttribute(): Collection
71
    {
72
        return self::where('parent_id', $this->parent_id)
0 ignored issues
show
Bug introduced by
The property parent_id does not seem to exist. Did you mean parent?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
            ->orderBy('order_column')
74
            ->get();
75
    }
76
77
    public function parent(): BelongsTo
78
    {
79
        return $this->belongsTo(self::class, 'parent_id');
80
    }
81
82
    public function hasParent(): bool
83
    {
84
        return ! is_null($this->parent);
85
    }
86
87
    public function getUrlAttribute(): string
88
    {
89
        $localeSegment = '';
90
91
        if (locales()->count() > 1) {
92
            $localeSegment = '/'.locale();
93
        }
94
95
        if ($this->technical_name === SpecialArticle::HOME) {
0 ignored issues
show
Documentation introduced by
The property technical_name does not exist on object<App\Models\Article>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
            return $localeSegment;
97
        }
98
99
        $parentSlug = $this->hasParent() ? $this->parent->slug.'/' : '';
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<App\Models\Article>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
101
        return "{$localeSegment}/{$parentSlug}{$this->slug}";
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<App\Models\Article>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
    }
103
}
104