Article::scopePublishedByBot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\Models;
12
13
use Carbon\Carbon;
14
use Illuminate\Support\Str;
15
use Illuminate\Database\Eloquent\Model;
16
use Illuminate\Database\Eloquent\Builder;
17
use Illuminate\Contracts\Auth\Authenticatable;
18
use Illuminate\Database\Eloquent\Relations\MorphTo;
19
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
20
21
/**
22
 * Class Article.
23
 */
24
class Article extends Model
25
{
26
    /**
27
     * Published date and time field name.
28
     */
29
    private const PUBLISHED_AT = 'published_at';
30
31
    /**
32
     * Directory of article images.
33
     */
34
    public const DEFAULT_IMAGE_PATH = '/static/articles/';
35
36
    /**
37
     * @var array
38
     */
39
    protected $dates = [
40
        self::PUBLISHED_AT,
41
    ];
42
43
    /**
44
     * @var string
45
     */
46
    protected $table = 'articles';
47
48
    /**
49
     * @var array
50
     */
51
    protected $fillable = [
52
        'user_id', 'title', 'image', 'content_source',
53
        'status', 'published_at',
54
    ];
55
56
    /**
57
     * @param  Builder $builder
58
     * @return Builder
59
     */
60
    public static function scopeLatestPublished(Builder $builder): Builder
61
    {
62
        return $builder
63
            ->with('user')
64
            ->with('tags')
65
            ->latest('published_at')
66
            ->published();
67
    }
68
69
    /**
70
     * @param  Builder $builder
71
     * @return Builder
72
     */
73
    public static function scopeLatest(Builder $builder): Builder
74
    {
75
        return $builder->orderBy('published_at', 'desc');
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
76
    }
77
78
    /**
79
     * @param  Builder $builder
80
     * @return Builder
81
     */
82
    public static function scopePublished(Builder $builder): Builder
83
    {
84
        return $builder
85
            ->where('published_at', '<=', Carbon::now())
86
            ->where('status', Article\Status::PUBLISHED);
87
    }
88
89
    /**
90
     * @param  Builder $builder
91
     * @return Builder
92
     */
93
    public static function scopePublishedByBot(Builder $builder): Builder
94
    {
95
        return $builder
96
            ->where('user_type', Bot::class);
97
    }
98
99
    /**
100
     * @param  Authenticatable|User|null $user
101
     * @return bool
102
     */
103
    public function isAllowedForUser(?Authenticatable $user): bool
104
    {
105
        $isAuthor = $user === null ? false : ($this->user->id === $user->getAuthIdentifier());
106
107
        $isPublished = $this->status === Article\Status::PUBLISHED;
108
109
        $isAllowPublishedTime = $this->published_at <= Carbon::now();
110
111
        return $isAuthor || ($isPublished && $isAllowPublishedTime);
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getImageUrlAttribute(): string
118
    {
119
        if (Str::startsWith($this->image, ['http:', 'https:', '//'])) {
120
            return $this->image;
121
        }
122
123
        return static::DEFAULT_IMAGE_PATH . $this->image;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getCapitalizeTitleAttribute(): string
130
    {
131
        return Str::ucfirst($this->title);
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getNicePublishedDateAttribute(): string
138
    {
139
        if ($this->published_at > Carbon::now()->subMonth()) {
140
            return $this->published_at->diffForHumans();
141
        }
142
143
        return $this->published_at->toDateTimeString();
144
    }
145
146
    /**
147
     * @return MorphTo
148
     */
149
    public function user(): MorphTo
150
    {
151
        return $this->morphTo();
152
    }
153
154
    /**
155
     * @return BelongsToMany
156
     */
157
    public function tags(): BelongsToMany
158
    {
159
        return $this->belongsToMany(Tag::class, 'article_tags');
160
    }
161
}
162