Completed
Push — master ( 2b6acf...5f05b1 )
by Christopher
03:20
created

PostScopes::scopePublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Chriscreate\Blog\Traits\Post;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder;
7
8
trait PostScopes
9
{
10
    use PostCategoryScopes,
11
    PostTagScopes;
12
13
    public function scopePublished(Builder $query)
14
    {
15
        return $query->where(function (Builder $query) {
16
            return $query->where('status', self::PUBLISHED)
17
            ->whereNotNull('published_at');
18
        })->orWhere(function (Builder $query) {
19
            return $query->where('status', self::SCHEDULED)
20
            ->where('published_at', '<=', Carbon::now());
21
        });
22
    }
23
24
    public function scopeScheduled(Builder $query)
25
    {
26
        return $query->where(function (Builder $query) {
27
            return $query->where('status', self::SCHEDULED)
28
            ->where('published_at', '>', Carbon::now());
29
        });
30
    }
31
32
    public function scopeDraft(Builder $query)
33
    {
34
        return $query->where(function (Builder $query) {
35
            return $query->where('status', self::DRAFT)
36
            ->whereNull('published_at');
37
        });
38
    }
39
40
    public function scopeNotPublished(Builder $query)
41
    {
42
        return $query->where(function (Builder $query) {
43
            return $query->draft();
44
        })->orWhere(function (Builder $query) {
45
            return $query->scheduled();
46
        });
47
    }
48
49
    public function scopeOrderByLatest(Builder $query)
50
    {
51
        return $query->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...
52
    }
53
54
    public function scopePublishedLastMonth(Builder $query, int $limit = 5)
55
    {
56
        return $query->whereBetween('published_at', [
57
            Carbon::now()->subMonth(), Carbon::now(),
58
        ])->orderByLatest()
59
        ->limit($limit);
60
    }
61
62
    public function scopePublishedLastWeek(Builder $query)
63
    {
64
        return $query->whereBetween('published_at', [
65
            Carbon::now()->subWeek(), Carbon::now(),
66
        ])->orderByLatest();
67
    }
68
}
69