Completed
Push — master ( 704c19...051415 )
by Sebastian
05:14 queued 55s
created

Article   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findByTechnicalName() 0 15 2
A getWithTechnicalNameLike() 0 11 1
A isDeletable() 0 4 1
1
<?php
2
3
namespace app\Models;
4
5
use App\Foundation\Models\Base\ModuleModel;
6
use App\Foundation\Models\Traits\HasSlug;
7
use Cache;
8
use Exception;
9
use Illuminate\Support\Collection;
10
11
class Article extends ModuleModel
12
{
13
    use HasSlug;
14
15
    protected $with = ['media'];
16
17
    public $mediaLibraryCollections = ['images', 'downloads'];
18
    public $translatable = ['name', 'text', 'url'];
19
20
    public static function findByTechnicalName(string $technicalName): Article
21
    {
22
        return Cache::rememberForever(
23
            "article.findByTechnicalName.{$technicalName}",
24
            function () use ($technicalName): Article {
25
                $article = static::where('technical_name', $technicalName)->first();
26
27
                if ($article === null) {
28
                    throw new Exception("Article `{$technicalName}` not found");
29
                }
30
31
                return $article;
32
            }
33
        );
34
    }
35
36
    public static function getWithTechnicalNameLike(string $technicalName): Collection
37
    {
38
        return Cache::rememberForever(
39
            "article.getWithTechnicalNameLike.{$technicalName}",
40
            function () use ($technicalName): Collection {
41
                return static::where('technical_name', 'like', "{$technicalName}.%")
42
                    ->orderBy('order_column')
43
                    ->get();
44
            }
45
        );
46
    }
47
48
    public function isDeletable(): bool
49
    {
50
        return !(bool) $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...
51
    }
52
}
53