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\Facades\Date; |
19
|
|
|
|
20
|
|
|
trait HasKeptFlagHelpers |
21
|
|
|
{ |
22
|
|
|
public function initializeHasKeptFlagHelpers(): void |
23
|
|
|
{ |
24
|
|
|
$this->casts['is_kept'] = 'boolean'; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* If entity is kept. |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function isKept(): bool |
33
|
|
|
{ |
34
|
|
|
return $this->getAttributeValue('is_kept'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* If entity is unkept. |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function isUnkept(): bool |
43
|
|
|
{ |
44
|
|
|
return !$this->isKept(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Mark entity as kept. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function keep(): void |
53
|
|
|
{ |
54
|
|
|
$this->setAttribute('is_kept', true); |
|
|
|
|
55
|
|
|
$this->save(); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->fireModelEvent('kept', false); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Mark entity as unkept. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function unkeep(): void |
66
|
|
|
{ |
67
|
|
|
$this->setAttribute('is_kept', false); |
68
|
|
|
if (property_exists($this, 'setKeptOnUpdate')) { |
69
|
|
|
$this->setKeptOnUpdate = false; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
$this->save(); |
72
|
|
|
|
73
|
|
|
$this->fireModelEvent('unkept', false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get unkept models that are older than the given number of hours. |
78
|
|
|
* |
79
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
80
|
|
|
* @param int $hours |
81
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
82
|
|
|
*/ |
83
|
|
|
public function scopeOnlyUnkeptOlderThanHours(Builder $builder, $hours) |
84
|
|
|
{ |
85
|
|
|
return $builder |
86
|
|
|
->withoutGlobalScope(KeptFlagScope::class) |
87
|
|
|
->where('is_kept', 0) |
88
|
|
|
->where(static::getCreatedAtColumn(), '<=', Date::now()->subHours($hours)->toDateTimeString()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|