1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Signifly\Cancellation\Traits; |
4
|
|
|
|
5
|
|
|
use Signifly\Cancellation\Scopes\CancellingScope; |
6
|
|
|
|
7
|
|
|
trait Cancellable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Boot the cancelable trait for a model. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public static function bootCancellable() |
15
|
|
|
{ |
16
|
|
|
static::addGlobalScope(new CancellingScope()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Perform the actual cancel query on this model instance. |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function cancel() |
25
|
|
|
{ |
26
|
|
|
if ($this->fireModelEvent('cancelling') === false) { |
|
|
|
|
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$time = $this->freshTimestamp(); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$this->{$this->getCancelledAtColumn()} = $time; |
33
|
|
|
$this->{$this->getUpdatedAtColumn()} = $time; |
|
|
|
|
34
|
|
|
|
35
|
|
|
$result = $this->save(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->fireModelEvent('cancelled', false); |
38
|
|
|
|
39
|
|
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register a restoring model event with the dispatcher. |
44
|
|
|
* |
45
|
|
|
* @param \Closure|string $callback |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public static function cancelling($callback) |
50
|
|
|
{ |
51
|
|
|
static::registerModelEvent('cancelling', $callback); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Register a restoring model event with the dispatcher. |
56
|
|
|
* |
57
|
|
|
* @param \Closure|string $callback |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public static function cancelled($callback) |
62
|
|
|
{ |
63
|
|
|
static::registerModelEvent('cancelled', $callback); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Determine if the model instance has been cancelled. |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isCancelled() |
72
|
|
|
{ |
73
|
|
|
return ! is_null($this->{$this->getCancelledAtColumn()}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Keep a cancelled model instance. |
78
|
|
|
* |
79
|
|
|
* @return bool|null |
80
|
|
|
*/ |
81
|
|
|
public function keep() |
82
|
|
|
{ |
83
|
|
|
// If the restoring event does not return false, we will proceed with this |
84
|
|
|
// restore operation. Otherwise, we bail out so the developer will stop |
85
|
|
|
// the restore totally. We will clear the deleted timestamp and save. |
86
|
|
|
if ($this->fireModelEvent('keeping') === false) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->{$this->getCancelledAtColumn()} = null; |
91
|
|
|
|
92
|
|
|
$result = $this->save(); |
93
|
|
|
|
94
|
|
|
$this->fireModelEvent('kept', false); |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Register a restoring model event with the dispatcher. |
101
|
|
|
* |
102
|
|
|
* @param \Closure|string $callback |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public static function keeping($callback) |
107
|
|
|
{ |
108
|
|
|
static::registerModelEvent('keeping', $callback); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Register a restored model event with the dispatcher. |
113
|
|
|
* |
114
|
|
|
* @param \Closure|string $callback |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public static function kept($callback) |
119
|
|
|
{ |
120
|
|
|
static::registerModelEvent('kept', $callback); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the name of the "cancelled at" column. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getCancelledAtColumn() |
129
|
|
|
{ |
130
|
|
|
return defined('static::CANCELLED_AT') ? static::CANCELLED_AT : 'cancelled_at'; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the fully qualified "cancelled at" column. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getQualifiedCancelledAtColumn() |
139
|
|
|
{ |
140
|
|
|
return $this->getTable().'.'.$this->getCancelledAtColumn(); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|