Passed
Push — main ( 3cfdb8...a45320 )
by PRATIK
10:34
created

Post::getStatusAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Adminetic\Website\Models\Admin;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User 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...
6
use Conner\Tagging\Taggable;
7
use Adminetic\Website\Traits\PostTrait;
8
use Adminetic\Category\Models\Admin\Category;
9
use Illuminate\Support\Facades\Cache;
10
use Illuminate\Database\Eloquent\Model;
11
use Cviebrock\EloquentSluggable\Sluggable;
12
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...
13
use CyrildeWit\EloquentViewable\Contracts\Viewable;
14
use CyrildeWit\EloquentViewable\InteractsWithViews;
15
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
16
17
class Post extends Model implements Viewable
18
{
19
    use LogsActivity, PostTrait, Sluggable, SluggableScopeHelpers, Taggable, InteractsWithViews;
20
21
    protected $guarded = [];
22
23
    // Forget cache on updating or saving and deleting
24
    public static function boot()
25
    {
26
        parent::boot();
27
28
        static::saving(function () {
29
            self::cacheKey();
30
        });
31
32
        static::deleting(function () {
33
            self::cacheKey();
34
        });
35
    }
36
37
    // Cache Keys
38
    private static function cacheKey()
39
    {
40
        Cache::has('posts') ? Cache::forget('posts') : '';
41
    }
42
43
    // Logs
44
    protected static $logName = 'post';
45
46
    // Casts
47
    protected $casts = [
48
        'meta_keywords' => 'array'
49
    ];
50
51
    /**
52
     * Return the sluggable configuration array for this model.
53
     *
54
     * @return array
55
     */
56
    public function sluggable(): array
57
    {
58
        return [
59
            'slug' => [
60
                'source' => 'name',
61
            ],
62
        ];
63
    }
64
65
    // Accessors
66
    public function getStatusAttribute($attribute)
67
    {
68
        return $attribute <= 3 ? [
69
            1 => 'Draft',
70
            2 => 'Pending',
71
            3 => 'Published'
72
        ][$attribute] : 'N/A';
73
    }
74
75
76
    // Relation
77
    public function author()
78
    {
79
        return $this->belongsTo(User::class, 'author_id');
80
    }
81
    public function category()
82
    {
83
        return $this->belongsTo(Category::class, 'category_id');
84
    }
85
86
    // Helper Function
87
    public function statusColor()
88
    {
89
        return $this->getRawOriginal('status') == 1 ? "danger" : ($this->getRawOriginal('status') == 2 ? "warning" : "success");
90
    }
91
}
92