Passed
Pull Request — master (#4)
by
unknown
14:26
created

ActivatableTrait::onlyDeactivated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace niclasleonbock\Eloquent;
3
4
use niclasleonbock\Eloquent\ActivatableScope;
5
6
trait ActivatableTrait
7
{
8
    protected $activatedAtColumn = 'activated_at';
9
10
    /**
11
     * Boot the activatable trait for the model.
12
     *
13
     * @return void
14
     */
15
    public static function bootActivatableTrait()
16
    {
17
        static::addGlobalScope(new ActivatableScope());
18
    }
19
20
//    /**
21
//     * Get a new query builder that includes deactivated data sets.
22
//     *
23
//     * @return \Illuminate\Database\Eloquent\Builder|static
24
//     */
25
//    public static function withDeactivated()
26
//    {
27
//        return (new static)->newQueryWithoutScope(new ActivatableScope());
28
//    }
29
30
//    /**
31
//     * Get a new query builder that only includes deactivated data sets.
32
//     *
33
//     * @return \Illuminate\Database\Eloquent\Builder|static
34
//     */
35
//    public static function onlyDeactivated()
36
//    {
37
//        $instance = new static;
38
//
39
//        $column = $instance->getQualifiedActivatedAtColumn();
40
//
41
//        return $instance->newQueryWithoutScope(new ActivatableScope())->whereNotNull($column);
42
//    }
43
44
    /**
45
     * Determine if the model instance is activated.
46
     *
47
     * @return bool
48
     */
49
    public function activated()
50
    {
51
        return !is_null($this->{$this->getActivatedAtColumn()});
52
    }
53
54
    /**
55
     * Determine if the model instance is activated. Alias for activated().
56
     *
57
     * @return bool
58
     */
59
    public function isActivated()
60
    {
61
        return $this->activated();
62
    }
63
64
    /**
65
     * Activate the given model instance.
66
     *
67
     * @return void
68
     */
69
    public function activate()
70
    {
71
        if (false === $this->fireModelEvent('activate')) {
72
            return false;
73
        }
74
75
        $query = $this->newQuery()->where($this->getKeyName(), $this->getKey());
76
77
        $this->{$this->getActivatedAtColumn()} = $time = $this->freshTimestamp();
78
79
        $query->update([ $this->getActivatedAtColumn() => $this->fromDateTime($time) ]);
80
    }
81
82
    /**
83
     * Deactivate the given model instance.
84
     *
85
     * @return void
86
     */
87
    public function deactivate()
88
    {
89
        if (false === $this->fireModelEvent('deactivate')) {
90
            return false;
91
        }
92
93
        $query = $this->newQuery()->where($this->getKeyName(), $this->getKey());
94
95
        $this->{$this->getActivatedAtColumn()} = null;
96
97
        $query->update([ $this->getActivatedAtColumn() => null ]);
98
    }
99
100
    /**
101
     * Get the name of the "activated at" column.
102
     *
103
     * @return string
104
     */
105
    public function getActivatedAtColumn()
106
    {
107
        return $this->activatedAtColumn;
108
    }
109
110
    /**
111
     * Get the fully qualified "activated at" column.
112
     *
113
     * @return string
114
     */
115
    public function getQualifiedActivatedAtColumn()
116
    {
117
        return $this->getTable() . '.' . $this->getActivatedAtColumn();
118
    }
119
}
120