Passed
Push — main ( 93d181...6c645c )
by PRATIK
15:49
created

Category   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 107
rs 10
c 1
b 0
f 0
wmc 21

16 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 1
A cacheKey() 0 5 4
A getActivitylogOptions() 0 3 1
A getIconImageAttribute() 0 3 2
A childrenCategories() 0 3 1
A parent() 0 3 1
A getImageAttribute() 0 3 2
A __construct() 0 5 1
A products() 0 3 1
A scopeWhoIsRootParent() 0 3 1
A categories() 0 3 1
A root_parent() 0 3 1
A scopeFeatured() 0 3 1
A scopeActive() 0 3 1
A scopePosition() 0 3 1
A scopeWhoIsParent() 0 3 1
1
<?php
2
3
namespace Adminetic\Website\Models\Admin;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Cache;
7
use Spatie\Activitylog\LogOptions;
0 ignored issues
show
Bug introduced by
The type Spatie\Activitylog\LogOptions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Spatie\Activitylog\Traits\LogsActivity;
0 ignored issues
show
Bug introduced by
The type Spatie\Activitylog\Traits\LogsActivity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Spatie\MediaLibrary\HasMedia;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\HasMedia was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Spatie\MediaLibrary\InteractsWithMedia;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\InteractsWithMedia was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class Category extends Model implements HasMedia
13
{
14
    use LogsActivity, InteractsWithMedia;
15
16
    protected $guarded = [];
17
18
    // Forget cache on updating or saving and deleting
19
    public static function boot()
20
    {
21
        parent::boot();
22
23
        static::saving(function () {
24
            self::cacheKey();
25
        });
26
27
        static::deleting(function () {
28
            self::cacheKey();
29
        });
30
    }
31
32
    // Cache Keys
33
    private static function cacheKey()
34
    {
35
        Cache::has('categories') ? Cache::forget('categories') : '';
36
        Cache::has('parent_categories') ? Cache::forget('parent_categories') : '';
37
        Cache::has('product_categories') ? Cache::forget('product_categories') : '';
38
    }
39
40
    // Logs
41
    protected static $logName = 'category';
42
43
    public function getActivitylogOptions(): LogOptions
44
    {
45
        return LogOptions::defaults();
46
    }
47
48
49
    public function __construct(array $attributes = [])
50
    {
51
        $this->table = config('website.table_prefix', 'website') . '_categories';
52
53
        parent::__construct($attributes);
54
    }
55
56
    // Relationship
57
    protected $parentColumn = 'parent_id';
58
59
    public function parent()
60
    {
61
        return $this->belongsTo(Category::class, $this->parentColumn);
62
    }
63
64
    public function root_parent()
65
    {
66
        return $this->belongsTo(Category::class, $this->root_parent_id);
0 ignored issues
show
Bug introduced by
The property root_parent_id does not exist on Adminetic\Website\Models\Admin\Category. Did you mean parent?
Loading history...
67
    }
68
69
    public function categories()
70
    {
71
        return $this->hasMany(Category::class, 'parent_id');
72
    }
73
74
    public function childrenCategories()
75
    {
76
        return $this->hasMany(Category::class, 'parent_id')->with('categories');
77
    }
78
79
    public function products()
80
    {
81
        return $this->hasMany(Product::class, 'category_id');
82
    }
83
84
    // Scopes
85
    public function scopeWhoIsParent($qry)
86
    {
87
        return $qry->whereNull('parent_id');
88
    }
89
90
    public function scopeWhoIsRootParent($qry)
91
    {
92
        return $qry->whoIsParent()->whereNull('root_parent_id');
93
    }
94
95
    public function scopePosition($qry)
96
    {
97
        return $qry->orderBy('position');
98
    }
99
100
    public function scopeActive($qry)
101
    {
102
        return $qry->where('active', 1);
103
    }
104
105
    public function scopeFeatured($qry)
106
    {
107
        return $qry->where('featured', 1);
108
    }
109
110
    // Accessors
111
    public function getImageAttribute()
112
    {
113
        return !is_null($this->getFirstMedia('image')) ? $this->getFirstMediaUrl('image') : null;
114
    }
115
116
    public function getIconImageAttribute()
117
    {
118
        return !is_null($this->getFirstMedia('icon_image')) ? $this->getFirstMediaUrl('icon_image') : null;
119
    }
120
}
121