ClosedFlagScope::apply()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
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 ClosedFlagScope implements Scope
21
{
22
    /**
23
     * All of the extensions to be added to the builder.
24
     *
25
     * @var array
26
     */
27
    protected $extensions = [
28
        'UndoClose',
29
        'Close',
30
        'WithClosed',
31
        'WithoutClosed',
32
        'OnlyClosed',
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, 'shouldApplyClosedFlagScope') && $model->shouldApplyClosedFlagScope()) {
45
            $builder->where('is_closed', 0);
46
        }
47
    }
48
49
    /**
50
     * Extend the query builder with the needed functions.
51
     *
52
     * @param \Illuminate\Database\Eloquent\Builder $builder
53
     * @return void
54
     */
55
    public function extend(Builder $builder): void
56
    {
57
        foreach ($this->extensions as $extension) {
58
            $this->{"add{$extension}"}($builder);
59
        }
60
    }
61
62
    /**
63
     * Add the `undoClose` extension to the builder.
64
     *
65
     * @param \Illuminate\Database\Eloquent\Builder $builder
66
     * @return void
67
     */
68
    protected function addUndoClose(Builder $builder): void
69
    {
70
        $builder->macro('undoClose', function (Builder $builder) {
71
            $builder->withClosed();
72
73
            return $builder->update(['is_closed' => 0]);
74
        });
75
    }
76
77
    /**
78
     * Add the `close` extension to the builder.
79
     *
80
     * @param \Illuminate\Database\Eloquent\Builder $builder
81
     * @return void
82
     */
83
    protected function addClose(Builder $builder): void
84
    {
85
        $builder->macro('close', function (Builder $builder) {
86
            return $builder->update(['is_closed' => 1]);
87
        });
88
    }
89
90
    /**
91
     * Add the `withClosed` extension to the builder.
92
     *
93
     * @param \Illuminate\Database\Eloquent\Builder $builder
94
     * @return void
95
     */
96
    protected function addWithClosed(Builder $builder): void
97
    {
98
        $builder->macro('withClosed', function (Builder $builder) {
99
            return $builder->withoutGlobalScope($this);
100
        });
101
    }
102
103
    /**
104
     * Add the `withoutClosed` extension to the builder.
105
     *
106
     * @param \Illuminate\Database\Eloquent\Builder $builder
107
     * @return void
108
     */
109
    protected function addWithoutClosed(Builder $builder): void
110
    {
111
        $builder->macro('withoutClosed', function (Builder $builder) {
112
            return $builder->withoutGlobalScope($this)->where('is_closed', 0);
113
        });
114
    }
115
116
    /**
117
     * Add the `onlyClosed` extension to the builder.
118
     *
119
     * @param \Illuminate\Database\Eloquent\Builder $builder
120
     * @return void
121
     */
122
    protected function addOnlyClosed(Builder $builder): void
123
    {
124
        $builder->macro('onlyClosed', function (Builder $builder) {
125
            return $builder->withoutGlobalScope($this)->where('is_closed', 1);
126
        });
127
    }
128
}
129