Passed
Push — master ( ae3e5c...e9cc44 )
by Anton
02:39
created

KeptFlagScope::addWithNotKept()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Scopes\Classic;
15
16
use Illuminate\Database\Eloquent\Builder;
17
use Illuminate\Database\Eloquent\Model;
18
use Illuminate\Database\Eloquent\Scope;
19
20
final class KeptFlagScope implements Scope
21
{
22
    /**
23
     * All of the extensions to be added to the builder.
24
     *
25
     * @var array
26
     */
27
    protected $extensions = [
28
        'Keep',
29
        'UndoKeep',
30
        'WithNotKept',
31
        'WithoutNotKept',
32
        'OnlyNotKept',
33
    ];
34
35
    /**
36
     * Apply the scope to a given Eloquent query builder.
37
     *
38
     * @param \Illuminate\Database\Eloquent\Builder $builder
39
     * @param \Illuminate\Database\Eloquent\Model $model
40
     * @return void
41
     */
42
    public function apply(Builder $builder, Model $model): void
43
    {
44
        $builder->where('is_kept', 1);
45
    }
46
47
    /**
48
     * Extend the query builder with the needed functions.
49
     *
50
     * @param \Illuminate\Database\Eloquent\Builder $builder
51
     * @return void
52
     */
53
    public function extend(Builder $builder): void
54
    {
55
        foreach ($this->extensions as $extension) {
56
            $this->{"add{$extension}"}($builder);
57
        }
58
    }
59
60
    /**
61
     * Add the `keep` extension to the builder.
62
     *
63
     * @param \Illuminate\Database\Eloquent\Builder $builder
64
     * @return void
65
     */
66
    protected function addKeep(Builder $builder): void
67
    {
68
        $builder->macro('keep', function (Builder $builder) {
69
            $builder->withNotKept();
70
71
            return $builder->update(['is_kept' => 1]);
72
        });
73
    }
74
75
    /**
76
     * Add the `undoKeep` extension to the builder.
77
     *
78
     * @param \Illuminate\Database\Eloquent\Builder $builder
79
     * @return void
80
     */
81
    protected function addUndoKeep(Builder $builder): void
82
    {
83
        $builder->macro('undoKeep', function (Builder $builder) {
84
            return $builder->update(['is_kept' => 0]);
85
        });
86
    }
87
88
    /**
89
     * Add the `withNotKept` extension to the builder.
90
     *
91
     * @param \Illuminate\Database\Eloquent\Builder $builder
92
     * @return void
93
     */
94
    protected function addWithNotKept(Builder $builder): void
95
    {
96
        $builder->macro('withNotKept', function (Builder $builder) {
97
            return $builder->withoutGlobalScope($this);
98
        });
99
    }
100
101
    /**
102
     * Add the `withoutNotKept` extension to the builder.
103
     *
104
     * @param \Illuminate\Database\Eloquent\Builder $builder
105
     * @return void
106
     */
107
    protected function addWithoutNotKept(Builder $builder): void
108
    {
109
        $builder->macro('withoutNotKept', function (Builder $builder) {
110
            return $builder->withoutGlobalScope($this)->where('is_kept', 1);
111
        });
112
    }
113
114
    /**
115
     * Add the `onlyNotKept` extension to the builder.
116
     *
117
     * @param \Illuminate\Database\Eloquent\Builder $builder
118
     * @return void
119
     */
120
    protected function addOnlyNotKept(Builder $builder): void
121
    {
122
        $builder->macro('onlyNotKept', function (Builder $builder) {
123
            return $builder->withoutGlobalScope($this)->where('is_kept', 0);
124
        });
125
    }
126
}
127