Completed
Push — master ( 538517...7219d5 )
by Anton
02:52
created

HasKeptFlagHelpers::isUnkept()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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';
0 ignored issues
show
Bug Best Practice introduced by
The property casts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
    }
26
27
    public function isKept(): bool
28
    {
29
        return $this->getAttributeValue('is_kept');
0 ignored issues
show
Bug introduced by
It seems like getAttributeValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        return $this->/** @scrutinizer ignore-call */ getAttributeValue('is_kept');
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $this->/** @scrutinizer ignore-call */ 
40
               setAttribute('is_kept', true);
Loading history...
40
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        $this->/** @scrutinizer ignore-call */ 
41
               save();
Loading history...
41
42
        $this->fireModelEvent('kept', false);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->/** @scrutinizer ignore-call */ 
43
               fireModelEvent('kept', false);
Loading history...
43
    }
44
45
    public function unkeep(): void
46
    {
47
        $this->setAttribute('is_kept', false);
48
        if (property_exists($this, 'setKeptOnUpdate')) {
49
            $this->setKeptOnUpdate = false;
0 ignored issues
show
Bug Best Practice introduced by
The property setKeptOnUpdate does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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