Completed
Push — master ( b9b899...9ffa78 )
by Scott
02:17
created

Timeable::getRawCarbonString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Bedard\Shop\Traits;
2
3
use Carbon\Carbon;
4
use DB;
5
use October\Rain\Database\Builder;
6
7
trait Timeable
8
{
9
    /**
10
     * Boot the timeable trait for a model.
11
     *
12
     * @return void
13
     */
14
    public static function bootTimeable()
15
    {
16
        static::extend(function ($model) {
17
            $model->addFillable('start_at', 'end_at');
18
            $model->addDateAttribute('start_at');
19
            $model->addDateAttribute('end_at');
20
        });
21
    }
22
23
    /**
24
     * Get the raw date string.
25
     *
26
     * @return \Illuminate\Database\Query\Expression
27
     */
28
    protected function getRawCarbonString()
29
    {
30
        return DB::raw("'".(string) Carbon::now()."'");
31
    }
32
33
    /**
34
     * Query models that are active.
35
     *
36
     * @param  \October\Rain\Database\Builder   $query
37
     * @return \October\Rain\Database\Builder
38
     */
39
    public function scopeIsActive(Builder $query)
40
    {
41
        return $query->where(function ($model) {
42
            return $model->isNotExpired()->isNotUpcoming();
43
        });
44
    }
45
46
    /**
47
     * Query models that are expired.
48
     *
49
     * @param  \October\Rain\Database\Builder   $query
50
     * @return \October\Rain\Database\Builder
51
     */
52
    public function scopeIsExpired(Builder $query)
53
    {
54
        return $query->where(function ($model) {
55
            return $model->whereNotNull('end_at')
56
                ->where('end_at', '<=', $this->getRawCarbonString());
57
        });
58
    }
59
60
    /**
61
     * Query models that are upcoming.
62
     *
63
     * @param  \October\Rain\Database\Builder   $query
64
     * @return \October\Rain\Database\Builder
65
     */
66
    public function scopeIsUpcoming(Builder $query)
67
    {
68
        return $query->where(function ($model) {
69
            return $model->whereNotNull('start_at')
70
                ->where('start_at', '>', $this->getRawCarbonString());
71
        });
72
    }
73
74
    /**
75
     * Query models that are not active.
76
     *
77
     * @param  \October\Rain\Database\Builder   $query
78
     * @return \October\Rain\Database\Builder
79
     */
80
    public function scopeIsNotActive(Builder $query)
81
    {
82
        return $query->where(function ($model) {
83
            return $model->isExpired()->orWhere(function ($q) {
84
                $q->isUpcoming();
85
            });
86
        });
87
    }
88
89
    /**
90
     * Query models that are not expired.
91
     *
92
     * @param  \October\Rain\Database\Builder   $query
93
     * @return \October\Rain\Database\Builder
94
     */
95
    public function scopeIsNotExpired(Builder $query)
96
    {
97
        return $query->where(function ($model) {
98
            return $model->whereNull('end_at')
99
                ->orWhere('end_at', '>', $this->getRawCarbonString());
100
        });
101
    }
102
103
    /**
104
     * Query models that are not upcoming.
105
     *
106
     * @param  \October\Rain\Database\Builder   $query
107
     * @return \October\Rain\Database\Builder
108
     */
109
    public function scopeIsNotUpcoming(Builder $query)
110
    {
111
        return $query->where(function ($model) {
112
            return $model->whereNull('start_at')
113
                ->orWhere('start_at', '<=', $this->getRawCarbonString());
114
        });
115
    }
116
}
117