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

Category::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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