Completed
Push — master ( 3838fd...656106 )
by Scott
02:37
created

Timeable::scopeIsNotUpcoming()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Bedard\Shop\Traits;
2
3
use Carbon\Carbon;
4
use October\Rain\Database\Builder;
5
6
trait Timeable
7
{
8
    /**
9
     * Query models that are active.
10
     *
11
     * @param  \October\Rain\Database\Builder   $query
12
     * @return \October\Rain\Database\Builder
13
     */
14
    public function scopeIsActive(Builder $query)
15
    {
16
        return $query->where(function ($model) {
17
            return $model->isNotExpired()->isNotUpcoming();
18
        });
19
    }
20
21
    /**
22
     * Query models that are expired.
23
     *
24
     * @param  \October\Rain\Database\Builder   $query
25
     * @return \October\Rain\Database\Builder
26
     */
27
    public function scopeIsExpired(Builder $query)
28
    {
29
        return $query->where(function ($model) {
30
            return $model->whereNotNull('end_at')
31
                ->where('end_at', '<=', (string) Carbon::now());
32
        });
33
    }
34
35
    /**
36
     * Query models that are upcoming.
37
     *
38
     * @param  \October\Rain\Database\Builder   $query
39
     * @return \October\Rain\Database\Builder
40
     */
41
    public function scopeIsUpcoming(Builder $query)
42
    {
43
        return $query->where(function ($model) {
44
            return $model->whereNotNull('start_at')
45
                ->where('start_at', '>', (string) Carbon::now());
46
        });
47
    }
48
49
    /**
50
     * Query models that are not active.
51
     *
52
     * @param  \October\Rain\Database\Builder   $query
53
     * @return \October\Rain\Database\Builder
54
     */
55
    public function scopeIsNotActive(Builder $query)
56
    {
57
        return $query->where(function ($model) {
58
            return $model->isExpired()->orWhere(function($q) {
59
                $q->isUpcoming();
60
            });
61
        });
62
    }
63
64
    /**
65
     * Query models that are not expired.
66
     *
67
     * @param  \October\Rain\Database\Builder   $query
68
     * @return \October\Rain\Database\Builder
69
     */
70
    public function scopeIsNotExpired(Builder $query)
71
    {
72
        return $query->where(function ($model) {
73
            return $model->whereNull('end_at')
74
                ->orWhere('end_at', '>', (string) Carbon::now());
75
        });
76
    }
77
    /**
78
     * Query models that are not upcoming.
79
     *
80
     * @param  \October\Rain\Database\Builder   $query
81
     * @return \October\Rain\Database\Builder
82
     */
83
    public function scopeIsNotUpcoming(Builder $query)
84
    {
85
        return $query->where(function ($model) {
86
            return $model->isExpired()->orWhere(function ($q) {
87
                $q->isActive();
88
            });
89
        });
90
    }
91
}
92