1 | <?php |
||
2 | |||
3 | namespace LaravelCancellable\Scopes; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Illuminate\Database\Eloquent\Builder; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Database\Eloquent\Scope; |
||
9 | |||
10 | class CancellableScope implements Scope |
||
11 | { |
||
12 | /** |
||
13 | * All the extensions to be added to the builder. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $extensions = ['Cancel', 'UnCancel', 'WithCancelled', 'WithoutCancelled', 'OnlyCancelled']; |
||
18 | |||
19 | /** |
||
20 | * Apply the scope to a given Eloquent query builder. |
||
21 | * |
||
22 | * @param Builder $builder |
||
23 | * @param Model $model |
||
24 | * @return void |
||
25 | */ |
||
26 | public function apply(Builder $builder, Model $model) |
||
27 | { |
||
28 | if (is_callable([$model, 'getQualifiedCancelledAtColumn'], true, $name)) { |
||
29 | $builder->whereNull($model->getQualifiedCancelledAtColumn()) |
||
30 | ->orWhere($model->getQualifiedCancelledAtColumn(), '>', Carbon::now()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Extend the query builder with the needed functions. |
||
36 | * |
||
37 | * @param Builder $builder |
||
38 | * @return void |
||
39 | */ |
||
40 | public function extend(Builder $builder) |
||
41 | { |
||
42 | foreach ($this->extensions as $extension) { |
||
43 | $this->{"add{$extension}"}($builder); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get the "cancelled at" column for the builder. |
||
49 | * |
||
50 | * @param Builder $builder |
||
51 | * @return string |
||
52 | */ |
||
53 | protected function getCancelledAtColumn(Builder $builder) |
||
54 | { |
||
55 | if (count((array) $builder->getQuery()->joins) > 0) { |
||
56 | return $builder->getModel()->getQualifiedCancelledAtColumn(); |
||
57 | } |
||
58 | |||
59 | return $builder->getModel()->getCancelledAtColumn(); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Add the cancel extension to the builder. |
||
64 | * |
||
65 | * @param Builder $builder |
||
66 | * @return void |
||
67 | */ |
||
68 | protected function addCancel(Builder $builder) |
||
69 | { |
||
70 | $builder->macro('cancel', function (Builder $builder) { |
||
71 | $column = $this->getCancelledAtColumn($builder); |
||
72 | |||
73 | return $builder->update([ |
||
74 | $column => $builder->getModel()->freshTimestampString(), |
||
75 | ]); |
||
76 | }); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Add the un-cancel extension to the builder. |
||
81 | * |
||
82 | * @param Builder $builder |
||
83 | * @return void |
||
84 | */ |
||
85 | protected function addUnCancel(Builder $builder) |
||
86 | { |
||
87 | $builder->macro('unCancel', function (Builder $builder) { |
||
88 | $builder->withCancelled(); |
||
89 | |||
90 | $column = $this->getCancelledAtColumn($builder); |
||
91 | |||
92 | return $builder->update([ |
||
93 | $column => null, |
||
94 | ]); |
||
95 | }); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Add the with-cancel extension to the builder. |
||
100 | * |
||
101 | * @param Builder $builder |
||
102 | * @return void |
||
103 | */ |
||
104 | protected function addWithCancelled(Builder $builder) |
||
105 | { |
||
106 | $builder->macro('withCancelled', function (Builder $builder, $withCancelled = true) { |
||
107 | if (! $withCancelled) { |
||
108 | return $builder->withoutCancelled(); |
||
109 | } |
||
110 | |||
111 | return $builder->withoutGlobalScope($this); |
||
112 | }); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Add the without-cancel extension to the builder. |
||
117 | * |
||
118 | * @param Builder $builder |
||
119 | * @return void |
||
120 | */ |
||
121 | protected function addWithoutCancelled(Builder $builder) |
||
122 | { |
||
123 | $builder->macro('withoutCancelled', function (Builder $builder) { |
||
124 | $model = $builder->getModel(); |
||
125 | |||
126 | return $builder->withoutGlobalScope($this)->whereNull( |
||
127 | $model->getQualifiedCancelledAtColumn() |
||
128 | )->orWhere($model->getQualifiedCancelledAtColumn(), '>', Carbon::now()); |
||
129 | }); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Add the only-cancel extension to the builder. |
||
134 | * |
||
135 | * @param Builder $builder |
||
136 | * @return void |
||
137 | */ |
||
138 | protected function addOnlyCancelled(Builder $builder) |
||
139 | { |
||
140 | $builder->macro('onlyCancelled', function (Builder $builder) { |
||
141 | $model = $builder->getModel(); |
||
142 | |||
143 | $builder->withoutGlobalScope($this)->where($model->getQualifiedCancelledAtColumn(), '<=', Carbon::now()); |
||
144 | |||
145 | return $builder; |
||
146 | }); |
||
147 | } |
||
148 | } |
||
149 |