Activatable::isActive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models\Traits;
2
3
/**
4
 * Class     Activatable
5
 *
6
 * @package  Arcanedev\LaravelAuth\Traits
7
 * @author   ARCANEDEV <[email protected]>
8
 *
9
 * @property  bool                 is_active
10
 * @property  \Carbon\Carbon|null  activated_at
11
 *
12
 * @method    bool  save(array $options = [])
13
 */
14
trait Activatable
15
{
16
    /* -----------------------------------------------------------------
17
     |  Getters & Setters
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Get the `is_active` attribute.
23
     *
24
     * @return bool
25
     */
26 15
    public function getIsActiveAttribute()
27
    {
28 15
        return $this->isActive();
29
    }
30
31
    /* -----------------------------------------------------------------
32
     |  Main Methods
33
     | -----------------------------------------------------------------
34
     */
35
36
    /**
37
     * Activate/deactivate the model.
38
     *
39
     * @param  bool  $active
40
     * @param  bool  $save
41
     *
42
     * @return bool
43
     */
44 9
    protected function switchActive($active, $save = true)
45
    {
46 9
        $this->forceFill(['activated_at' => boolval($active) ? now() : null]);
0 ignored issues
show
Bug introduced by
It seems like forceFill() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
47
48 9
        return $save ? $this->save() : false;
49
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Check Methods
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Check if the model is active.
58
     *
59
     * @return bool
60
     */
61 75
    public function isActive()
62
    {
63 75
        return ! is_null($this->activated_at);
64
    }
65
}
66