Passed
Push — main ( bb1631...b48e8d )
by PRATIK
07:52 queued 04:02
created

Category::getIconImageAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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