Passed
Pull Request — main (#31)
by PRATIK
04:05
created

Category   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 20

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getActivitylogOptions() 0 3 1
A scopeActive() 0 3 1
A boot() 0 10 1
A getIconImageAttribute() 0 3 2
A childrenCategories() 0 3 1
A parent() 0 3 1
A getImageAttribute() 0 3 2
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 cacheKey() 0 5 4
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
41
    // Logs
42
    protected static $logName = 'category';
43
44
    public function getActivitylogOptions(): LogOptions
45
    {
46
        return LogOptions::defaults();
47
    }
48
49
    // Relationship
50
    protected $parentColumn = 'parent_id';
51
52
    public function parent()
53
    {
54
        return $this->belongsTo(Category::class, $this->parentColumn);
55
    }
56
57
    public function root_parent()
58
    {
59
        return $this->belongsTo(Category::class, $this->root_parent_id);
60
    }
61
62
    public function categories()
63
    {
64
        return $this->hasMany(Category::class, 'parent_id');
65
    }
66
67
    public function childrenCategories()
68
    {
69
        return $this->hasMany(Category::class, 'parent_id')->with('categories');
70
    }
71
72
    public function products()
73
    {
74
        return $this->hasMany(Product::class,'category_id');
75
    }
76
77
    // Scopes
78
    public function scopeWhoIsParent($qry)
79
    {
80
        return $qry->whereNull('parent_id');
81
    }
82
83
    public function scopeWhoIsRootParent($qry)
84
    {
85
        return $qry->whoIsParent()->whereNull('root_parent_id');
86
    }
87
88
    public function scopePosition($qry)
89
    {
90
        return $qry->orderBy('position');
91
    }
92
93
    public function scopeActive($qry)
94
    {
95
        return $qry->where('active', 1);
96
    }
97
98
    public function scopeFeatured($qry)
99
    {
100
        return $qry->where('featured', 1);
101
    }
102
103
    // Accessors
104
    public function getImageAttribute()
105
    {
106
        return !is_null($this->getFirstMedia('image')) ? $this->getFirstMediaUrl('image') : null;
107
    }
108
109
    public function getIconImageAttribute()
110
    {
111
        return !is_null($this->getFirstMedia('icon_image')) ? $this->getFirstMediaUrl('icon_image') : null;
112
    }
113
}
114