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
|
|
|
public function isKept(): bool |
28
|
|
|
{ |
29
|
|
|
return $this->getAttributeValue('is_kept'); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function isNotKept(): bool |
33
|
|
|
{ |
34
|
|
|
return !$this->isKept(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function keep(): void |
38
|
|
|
{ |
39
|
|
|
$this->setAttribute('is_kept', true); |
|
|
|
|
40
|
|
|
$this->save(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$this->fireModelEvent('kept', false); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function unkeep(): void |
46
|
|
|
{ |
47
|
|
|
$this->setAttribute('is_kept', false); |
48
|
|
|
if (property_exists($this, 'setKeptOnUpdate')) { |
49
|
|
|
$this->setKeptOnUpdate = false; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
$this->save(); |
52
|
|
|
|
53
|
|
|
$this->fireModelEvent('unkept', false); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get unkept models that are older than the given number of hours. |
58
|
|
|
* |
59
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
60
|
|
|
* @param int $hours |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
62
|
|
|
*/ |
63
|
|
|
public function scopeOnlyUnkeptOlderThanHours(Builder $builder, $hours) |
64
|
|
|
{ |
65
|
|
|
return $builder |
66
|
|
|
->withoutGlobalScope(KeptFlagScope::class) |
67
|
|
|
->where('is_kept', 0) |
68
|
|
|
->where(static::getCreatedAtColumn(), '<=', Date::now()->subHours($hours)->toDateTimeString()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|