Completed
Push — master ( 3dacf3...538517 )
by Anton
13s
created

HasExpiredAtHelpers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeHasExpiredAtHelpers() 0 3 1
A isUnexpired() 0 3 1
A unexpire() 0 6 1
A isExpired() 0 3 1
A expire() 0 6 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\Inverse;
15
16
use Illuminate\Support\Facades\Date;
17
18
trait HasExpiredAtHelpers
19
{
20
    public function initializeHasExpiredAtHelpers(): void
21
    {
22
        $this->dates[] = 'expired_at';
0 ignored issues
show
Bug Best Practice introduced by
The property dates does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
    }
24
25
    /**
26
     * If entity is expired.
27
     *
28
     * @return bool
29
     */
30
    public function isExpired(): bool
31
    {
32
        return !is_null($this->getAttributeValue('expired_at'));
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

32
        return !is_null($this->/** @scrutinizer ignore-call */ getAttributeValue('expired_at'));
Loading history...
33
    }
34
35
    /**
36
     * If entity is opened.
37
     *
38
     * @return bool
39
     */
40
    public function isUnexpired(): bool
41
    {
42
        return !$this->isExpired();
43
    }
44
45
    /**
46
     * Mark entity as expired.
47
     *
48
     * @return void
49
     */
50
    public function expire(): void
51
    {
52
        $this->setAttribute('expired_at', Date::now());
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

52
        $this->/** @scrutinizer ignore-call */ 
53
               setAttribute('expired_at', Date::now());
Loading history...
53
        $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

53
        $this->/** @scrutinizer ignore-call */ 
54
               save();
Loading history...
54
55
        $this->fireModelEvent('expired', 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

55
        $this->/** @scrutinizer ignore-call */ 
56
               fireModelEvent('expired', false);
Loading history...
56
    }
57
58
    /**
59
     * Mark entity as opened.
60
     *
61
     * @return void
62
     */
63
    public function unexpire(): void
64
    {
65
        $this->setAttribute('expired_at', null);
66
        $this->save();
67
68
        $this->fireModelEvent('unexpired', false);
69
    }
70
}
71