1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Eloquent Flag. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Flag\Traits\Classic; |
15
|
|
|
|
16
|
|
|
use Cog\Flag\Scopes\Classic\KeptFlagScope; |
17
|
|
|
use Illuminate\Database\Eloquent\Builder; |
18
|
|
|
use Illuminate\Support\Carbon; |
19
|
|
|
|
20
|
|
|
trait HasKeptFlagHelpers |
21
|
|
|
{ |
22
|
|
|
public function initializeHasKeptFlagHelpers(): void |
23
|
|
|
{ |
24
|
|
|
$this->casts['is_kept'] = 'boolean'; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set kept flag. |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
public function setKeptFlag() |
33
|
|
|
{ |
34
|
|
|
$this->setAttribute('is_kept', true); |
|
|
|
|
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Unset kept flag. |
41
|
|
|
* |
42
|
|
|
* @return static |
43
|
|
|
*/ |
44
|
|
|
public function unsetKeptFlag() |
45
|
|
|
{ |
46
|
|
|
$this->setAttribute('is_kept', false); |
47
|
|
|
|
48
|
|
|
if (property_exists($this, 'setKeptOnUpdate')) { |
49
|
|
|
$this->setKeptOnUpdate = false; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* If entity is kept. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function isKept(): bool |
61
|
|
|
{ |
62
|
|
|
return $this->getAttributeValue('is_kept'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* If entity is unkept. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function isUnkept(): bool |
71
|
|
|
{ |
72
|
|
|
return !$this->isKept(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Mark entity as kept. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function keep(): void |
81
|
|
|
{ |
82
|
|
|
$this->setKeptFlag()->save(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$this->fireModelEvent('kept', false); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Mark entity as unkept. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function unkeep(): void |
93
|
|
|
{ |
94
|
|
|
$this->unsetKeptFlag()->save(); |
95
|
|
|
|
96
|
|
|
$this->fireModelEvent('unkept', false); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get unkept models that are older than the given number of hours. |
101
|
|
|
* |
102
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
103
|
|
|
* @param int $hours |
104
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
105
|
|
|
*/ |
106
|
|
|
public function scopeOnlyUnkeptOlderThanHours(Builder $builder, $hours) |
107
|
|
|
{ |
108
|
|
|
return $builder |
109
|
|
|
->withoutGlobalScope(KeptFlagScope::class) |
110
|
|
|
->where('is_kept', 0) |
111
|
|
|
->where(static::getCreatedAtColumn(), '<=', Carbon::now()->subHours($hours)->toDateTimeString()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|