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

PostScopes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A scopePublished() 0 10 1
A scopeScheduled() 0 7 1
A scopeDraft() 0 7 1
A scopeNotPublished() 0 8 1
A scopeOrderByLatest() 0 4 1
A scopePublishedLastMonth() 0 7 1
A scopePublishedLastWeek() 0 6 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