Completed
Push — master ( 8dafd6...46bcfe )
by Anton
02:53
created

HasKeptFlagHelpers::initializeHasKeptFlagHelpers()   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\Carbon;
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
    /**
28
     * Set kept flag.
29
     *
30
     * @return static
31
     */
32
    public function setKeptFlag()
33
    {
34
        $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

34
        $this->/** @scrutinizer ignore-call */ 
35
               setAttribute('is_kept', true);
Loading history...
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;
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
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');
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

62
        return $this->/** @scrutinizer ignore-call */ getAttributeValue('is_kept');
Loading history...
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();
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

82
        $this->setKeptFlag()->/** @scrutinizer ignore-call */ save();
Loading history...
83
84
        $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

84
        $this->/** @scrutinizer ignore-call */ 
85
               fireModelEvent('kept', false);
Loading history...
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