Issues (927)

src/Models/Admin/Post.php (12 issues)

1
<?php
2
3
namespace Adminetic\Website\Models\Admin;
4
5
use App\Models\User;
0 ignored issues
show
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
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
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
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
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
    // Relationship
66
    public function category()
67
    {
68
        return $this->belongsTo(Category::class);
69
    }
70
71
    public function author()
72
    {
73
        return $this->belongsTo(User::class, 'user_id');
74
    }
75
76
    public function approvedBy()
77
    {
78
        return $this->belongsTo(User::class, 'approved_by');
79
    }
80
81
    public function tags(): MorphToMany
82
    {
83
        return $this->morphToMany(Tag::class, 'taggable');
84
    }
85
86
    // Accessors
87
    public function getImageAttribute()
88
    {
89
        return ! is_null($this->getFirstMedia('image')) ? $this->getFirstMediaUrl('image') : asset('adminetic/static/blog.gif');
90
    }
91
92
    public function getOptimizedTitleAttribute()
93
    {
94
        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...
95
    }
96
97
    public function getOptimizedExcerptAttribute()
98
    {
99
        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...
100
    }
101
102
    public function getStatusAttribute($attribute)
103
    {
104
        return in_array($attribute ?? null, [0, 1, 2, 3])
105
            ? [
106
                0 => 'Not Approved',
107
                1 => 'Published',
108
                2 => 'Pending',
109
                3 => 'Draft',
110
            ][$attribute] : null;
111
    }
112
113
    // Scopes
114
    public function scopeNotApproved($qry)
115
    {
116
        return $qry->where('status', 0);
117
    }
118
119
    public function scopePublished($qry)
120
    {
121
        return $qry->where('status', 1);
122
    }
123
124
    public function scopePending($qry)
125
    {
126
        return $qry->where('status', 2);
127
    }
128
129
    public function scopeDraft($qry)
130
    {
131
        return $qry->where('status', 3);
132
    }
133
134
    public function scopePriority($qry)
135
    {
136
        return $qry->orderBy('position');
137
    }
138
139
    public function scopeFeatured($qry)
140
    {
141
        return $qry->where('featured', 1);
142
    }
143
144
    // Helper Functions
145
    public function getStatusColor()
146
    {
147
        return in_array($this->getRawOriginal('status') ?? null, [0, 1, 2, 3]) ?
148
            [
149
                0 => 'danger',
150
                1 => 'success',
151
                2 => 'info',
152
                3 => 'warning',
153
            ][$this->getRawOriginal('status')] : null;
154
    }
155
156
    public function searchSchema()
157
    {
158
        $schema = Schema::article()
159
            ->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...
160
            ->description($this->meta_description ?? $this->excerpt)
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...
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...
161
            ->if(! is_null($this->getFirstMedia('image')), function (Article $schema) {
162
                $schema->image($this->getFirstMediaUrl('image'));
163
            })
164
            ->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...
165
166
        return $schema->toScript();
167
    }
168
}
169