Passed
Pull Request — master (#50)
by Anton
02:19
created

HasKeptFlagHelpers   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeHasKeptFlagHelpers() 0 3 1
A unkeep() 0 9 2
A scopeOnlyUnkeptOlderThanHours() 0 6 1
A keep() 0 6 1
A isKept() 0 3 1
A isUnkept() 0 3 1
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
    /**
28
     * If entity is kept.
29
     *
30
     * @return bool
31
     */
32
    public function isKept(): bool
33
    {
34
        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

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

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

55
        $this->/** @scrutinizer ignore-call */ 
56
               save();
Loading history...
56
57
        $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

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