SoftEnablingScope::addDisable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace MarcoTisi\SoftEnable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Scope;
7
use Illuminate\Database\Eloquent\Builder;
8
9
class SoftEnablingScope implements Scope
10
{
11
    /**
12
     * All of the extensions to be added to the builder.
13
     *
14
     * @var array
15
     */
16
    protected $extensions = ['Enable', 'Disable', 'WithDisabled', 'WithoutDisabled', 'OnlyDisabled'];
17
18
    /**
19
     * Apply the scope to a given Eloquent query builder.
20
     *
21
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
22
     * @param  \Illuminate\Database\Eloquent\Model  $model
23
     * @return void
24
     */
25 24
    public function apply(Builder $builder, Model $model)
26
    {
27 24
        $builder->where($model->getQualifiedEnabledColumn(), true);
28 24
    }
29
30
    /**
31
     * Extend the query builder with the needed functions.
32
     *
33
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
34
     * @return void
35
     */
36 29
    public function extend(Builder $builder)
37
    {
38 29
        foreach ($this->extensions as $extension) {
39 29
            $this->{"add{$extension}"}($builder);
40 29
        }
41 29
    }
42
43
    /**
44
     * Add the enable extension to the builder.
45
     *
46
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
47
     * @return void
48
     */
49 29
    protected function addEnable(Builder $builder)
50
    {
51
        $builder->macro('enable', function (Builder $builder) {
52 3
            $builder->withDisabled();
53
54 3
            return $builder->update([$builder->getModel()->getEnabledColumn() => true]);
55 29
        });
56 29
    }
57
58
    /**
59
     * Add the disable extension to the builder.
60
     *
61
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
62
     * @return void
63
     */
64 29
    protected function addDisable(Builder $builder)
65
    {
66
        $builder->macro('disable', function (Builder $builder) {
67 4
            $builder->withDisabled();
68
69 4
            return $builder->update([$builder->getModel()->getEnabledColumn() => false]);
70 29
        });
71 29
    }
72
73
    /**
74
     * Add the with-disabled extension to the builder.
75
     *
76
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
77
     * @return void
78
     */
79 29
    protected function addWithDisabled(Builder $builder)
80
    {
81
        $builder->macro('withDisabled', function (Builder $builder) {
82 18
            return $builder->withoutGlobalScope($this);
83 29
        });
84 29
    }
85
86
    /**
87
     * Add the without-disabled extension to the builder.
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
90
     * @return void
91
     */
92 29
    protected function addWithoutDisabled(Builder $builder)
93
    {
94
        $builder->macro('withoutDisabled', function (Builder $builder) {
95 1
            $model = $builder->getModel();
96
97 1
            $builder->withoutGlobalScope($this)->where(
98 1
                $model->getQualifiedEnabledColumn(),
99
                true
100 1
            );
101
102 1
            return $builder;
103 29
        });
104 29
    }
105
106
    /**
107
     * Add the only-disabled extension to the builder.
108
     *
109
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
110
     * @return void
111
     */
112
    protected function addOnlyDisabled(Builder $builder)
113
    {
114 29
        $builder->macro('onlyDisabled', function (Builder $builder) {
115 3
            $model = $builder->getModel();
116
117 3
            $builder->withoutGlobalScope($this)->where(
118 3
                $model->getQualifiedEnabledColumn(),
119
                false
120 3
            );
121
122 3
            return $builder;
123 29
        });
124 29
    }
125
}
126