Post::getSlugOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
nc 1
nop 0
dl 0
loc 5
c 1
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace Wingsline\Blog\Posts;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Auth;
8
use Spatie\Feed\Feedable;
9
use Spatie\Feed\FeedItem;
10
use Spatie\MediaLibrary\HasMedia;
11
use Spatie\MediaLibrary\InteractsWithMedia;
12
use Spatie\Sluggable\HasSlug;
0 ignored issues
show
Bug introduced by
The type Spatie\Sluggable\HasSlug 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\Sluggable\SlugOptions;
14
use Spatie\Tags\HasTags;
15
use Wingsline\Blog\Markdown\Markdown;
16
17
class Post extends Model implements Feedable, HasMedia
18
{
19
    use HasSlug;
20
    use HasTags;
21
    use InteractsWithMedia;
22
    use PostPresenter;
23
24
    public $casts = [
25
        'text' => '',
26
        'published' => 'boolean',
27
        'original_content' => 'boolean',
28
    ];
29
30
    public $dates = ['publish_date'];
31
    public $with = ['tags'];
32
33
    /**
34
     * The table associated with the model.
35
     *
36
     * @var string
37
     */
38
    protected $table = 'posts';
39
40
    public static function getFeedItems()
41
    {
42
        return static::published()
43
            ->public()
44
            ->orderBy('publish_date', 'desc')
45
            ->limit(100)
46
            ->get();
47
    }
48
49
    /**
50
     * Markdown accessor.
51
     *
52
     * @return string
53
     */
54
    public function getMarkdownAttribute()
55
    {
56
        return $this->getRawOriginal('text');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRawOriginal('text') also could return the type array which is incompatible with the documented return type string.
Loading history...
57
    }
58
59
    /**
60
     * Slug options.
61
     */
62
    public function getSlugOptions(): SlugOptions
63
    {
64
        return SlugOptions::create()
65
            ->generateSlugsFrom('title')
66
            ->saveSlugsTo('slug');
67
    }
68
69
    /**
70
     * Text accessor.
71
     *
72
     * @param string $original
73
     *
74
     * @return string
75
     */
76
    public function getTextAttribute($original)
77
    {
78
        return Markdown::convertWithParser($original);
79
    }
80
81
    public function scopePublic(Builder $query)
82
    {
83
        $query->where('publish_date', '<=', now());
84
    }
85
86
    public function scopePublished(Builder $query)
87
    {
88
        $query->where('published', true);
89
    }
90
91
    /**
92
     * @return array|\Spatie\Feed\FeedItem
93
     */
94
    public function toFeedItem()
95
    {
96
        return FeedItem::create()
97
            ->id($this->id)
98
            ->title($this->formatted_title)
0 ignored issues
show
Bug Best Practice introduced by
The property formatted_title does not exist on Wingsline\Blog\Posts\Post. Did you maybe forget to declare it?
Loading history...
99
            ->summary($this->text)
100
            ->updated($this->publish_date)
101
            ->link($this->url)
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist on Wingsline\Blog\Posts\Post. Did you maybe forget to declare it?
Loading history...
102
            ->author($this->author);
103
    }
104
105
    /**
106
     * Update model attributes from the form.
107
     *
108
     * @return static
109
     */
110
    public function updateAttributes(array $attributes)
111
    {
112
        $this->title = $attributes['title'];
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
113
        $this->text = $attributes['text'];
0 ignored issues
show
Bug Best Practice introduced by
The property text does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        $this->publish_date = $attributes['publish_date'];
0 ignored issues
show
Bug Best Practice introduced by
The property publish_date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
115
        $this->published = $attributes['published'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
116
        $this->original_content = $attributes['original_content'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The property original_content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
        $this->external_url = $attributes['external_url'];
0 ignored issues
show
Bug Best Practice introduced by
The property external_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
118
        $this->author = Auth::user()->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug Best Practice introduced by
The property author does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
120
        $this->save();
121
122
        if ($attributes['tags_text']) {
123
            $tags = array_map(function (string $tag) {
124
                return trim(strtolower($tag));
125
            }, explode(',', $attributes['tags_text']));
126
127
            $this->syncTags($tags);
128
        }
129
130
        return $this;
131
    }
132
}
133