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

Post::__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 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 Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8
use Illuminate\Support\Facades\Cache;
9
use Illuminate\Support\Str;
10
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...
11
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...
12
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...
13
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...
14
use Spatie\SchemaOrg\Article;
15
use Spatie\SchemaOrg\Schema;
16
17
class Post extends Model implements HasMedia
18
{
19
    use LogsActivity, InteractsWithMedia;
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
    public function getActivitylogOptions(): LogOptions
47
    {
48
        return LogOptions::defaults();
49
    }
50
51
    // Casts
52
    protected $casts = [
53
        'videos' => 'array',
54
    ];
55
56
    protected $appends = ['image', 'optimized_title'];
57
58
    public function __construct(array $attributes = [])
59
    {
60
        $this->table = config('website.table_prefix', 'website') . '_posts';
61
62
        parent::__construct($attributes);
63
    }
64
65
66
    // Relationship
67
    public function category()
68
    {
69
        return $this->belongsTo(Category::class);
70
    }
71
72
    public function author()
73
    {
74
        return $this->belongsTo(User::class, 'user_id');
75
    }
76
77
    public function approvedBy()
78
    {
79
        return $this->belongsTo(User::class, 'approved_by');
80
    }
81
82
    public function tags(): MorphToMany
83
    {
84
        return $this->morphToMany(Tag::class, 'taggable');
85
    }
86
87
    // Accessors
88
    public function getImageAttribute()
89
    {
90
        return !is_null($this->getFirstMedia('image')) ? $this->getFirstMediaUrl('image') : asset('adminetic/static/blog.gif');
91
    }
92
93
    public function getOptimizedTitleAttribute()
94
    {
95
        return Str::limit($this->name, 120, '...');
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
96
    }
97
98
    public function getOptimizedExcerptAttribute()
99
    {
100
        return Str::limit($this->excerpt, 255, '...');
0 ignored issues
show
Bug Best Practice introduced by
The property excerpt does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
101
    }
102
103
    public function getStatusAttribute($attribute)
104
    {
105
        return in_array($attribute ?? null, [0, 1, 2, 3])
106
            ? [
107
                0 => 'Not Approved',
108
                1 => 'Published',
109
                2 => 'Pending',
110
                3 => 'Draft',
111
            ][$attribute] : null;
112
    }
113
114
    // Scopes
115
    public function scopeNotApproved($qry)
116
    {
117
        return $qry->where('status', 0);
118
    }
119
120
    public function scopePublished($qry)
121
    {
122
        return $qry->where('status', 1);
123
    }
124
125
    public function scopePending($qry)
126
    {
127
        return $qry->where('status', 2);
128
    }
129
130
    public function scopeDraft($qry)
131
    {
132
        return $qry->where('status', 3);
133
    }
134
135
    public function scopePriority($qry)
136
    {
137
        return $qry->orderBy('position');
138
    }
139
140
    public function scopeFeatured($qry)
141
    {
142
        return $qry->where('featured', 1);
143
    }
144
145
    // Helper Functions
146
    public function getStatusColor()
147
    {
148
        return in_array($this->getRawOriginal('status') ?? null, [0, 1, 2, 3]) ?
149
            [
150
                0 => 'danger',
151
                1 => 'success',
152
                2 => 'info',
153
                3 => 'warning',
154
            ][$this->getRawOriginal('status')] : null;
155
    }
156
157
    public function searchSchema()
158
    {
159
        $schema = Schema::article()
160
            ->name($this->meta_name ?? $this->name)
0 ignored issues
show
Bug Best Practice introduced by
The property meta_name does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property name does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
161
            ->description($this->meta_description ?? $this->excerpt)
0 ignored issues
show
Bug Best Practice introduced by
The property meta_description does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property excerpt does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
162
            ->if(!is_null($this->getFirstMedia('image')), function (Article $schema) {
163
                $schema->image($this->getFirstMediaUrl('image'));
164
            })
165
            ->keywords($this->meta_keywords);
0 ignored issues
show
Bug Best Practice introduced by
The property meta_keywords does not exist on Adminetic\Website\Models\Admin\Post. Did you maybe forget to declare it?
Loading history...
166
167
        return $schema->toScript();
168
    }
169
}
170