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

EndedFlagScope::addUndoEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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\Inverse;
15
16
use Illuminate\Database\Eloquent\Builder;
17
use Illuminate\Database\Eloquent\Model;
18
use Illuminate\Database\Eloquent\Scope;
19
20
final class EndedFlagScope implements Scope
21
{
22
    /**
23
     * All of the extensions to be added to the builder.
24
     *
25
     * @var array
26
     */
27
    protected $extensions = [
28
        'UndoEnd',
29
        'End',
30
        'WithEnded',
31
        'WithoutEnded',
32
        'OnlyEnded',
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
        if (method_exists($model, 'shouldApplyEndedFlagScope') && !$model->shouldApplyEndedFlagScope()) {
45
            return;
46
        }
47
48
        $builder->where('is_ended', 0);
49
    }
50
51
    /**
52
     * Extend the query builder with the needed functions.
53
     *
54
     * @param \Illuminate\Database\Eloquent\Builder $builder
55
     * @return void
56
     */
57
    public function extend(Builder $builder): void
58
    {
59
        foreach ($this->extensions as $extension) {
60
            $this->{"add{$extension}"}($builder);
61
        }
62
    }
63
64
    /**
65
     * Add the `undoEnd` extension to the builder.
66
     *
67
     * @param \Illuminate\Database\Eloquent\Builder $builder
68
     * @return void
69
     */
70
    protected function addUndoEnd(Builder $builder): void
71
    {
72
        $builder->macro('undoEnd', function (Builder $builder) {
73
            $builder->withEnded();
74
75
            return $builder->update(['is_ended' => 0]);
76
        });
77
    }
78
79
    /**
80
     * Add the `end` extension to the builder.
81
     *
82
     * @param \Illuminate\Database\Eloquent\Builder $builder
83
     * @return void
84
     */
85
    protected function addEnd(Builder $builder): void
86
    {
87
        $builder->macro('end', function (Builder $builder) {
88
            return $builder->update(['is_ended' => 1]);
89
        });
90
    }
91
92
    /**
93
     * Add the `withEnded` extension to the builder.
94
     *
95
     * @param \Illuminate\Database\Eloquent\Builder $builder
96
     * @return void
97
     */
98
    protected function addWithEnded(Builder $builder): void
99
    {
100
        $builder->macro('withEnded', function (Builder $builder) {
101
            return $builder->withoutGlobalScope($this);
102
        });
103
    }
104
105
    /**
106
     * Add the `withoutEnded` extension to the builder.
107
     *
108
     * @param \Illuminate\Database\Eloquent\Builder $builder
109
     * @return void
110
     */
111
    protected function addWithoutEnded(Builder $builder): void
112
    {
113
        $builder->macro('withoutEnded', function (Builder $builder) {
114
            return $builder->withoutGlobalScope($this)->where('is_ended', 0);
115
        });
116
    }
117
118
    /**
119
     * Add the `onlyEnded` extension to the builder.
120
     *
121
     * @param \Illuminate\Database\Eloquent\Builder $builder
122
     * @return void
123
     */
124
    protected function addOnlyEnded(Builder $builder): void
125
    {
126
        $builder->macro('onlyEnded', function (Builder $builder) {
127
            return $builder->withoutGlobalScope($this)->where('is_ended', 1);
128
        });
129
    }
130
}
131