ApprovedAtScope::addOnlyNotApproved()   A
last analyzed

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
use Illuminate\Support\Facades\Date;
20
21
final class ApprovedAtScope implements Scope
22
{
23
    /**
24
     * All of the extensions to be added to the builder.
25
     *
26
     * @var array
27
     */
28
    protected $extensions = [
29
        'Approve',
30
        'UndoApprove',
31
        'WithNotApproved',
32
        'WithoutNotApproved',
33
        'OnlyNotApproved',
34
    ];
35
36
    /**
37
     * Apply the scope to a given Eloquent query builder.
38
     *
39
     * @param \Illuminate\Database\Eloquent\Builder $builder
40
     * @param \Illuminate\Database\Eloquent\Model $model
41
     * @return void
42
     */
43
    public function apply(Builder $builder, Model $model): void
44
    {
45
        if (method_exists($model, 'shouldApplyApprovedAtScope') && $model->shouldApplyApprovedAtScope()) {
46
            $builder->whereNotNull('approved_at');
47
        }
48
    }
49
50
    /**
51
     * Extend the query builder with the needed functions.
52
     *
53
     * @param \Illuminate\Database\Eloquent\Builder $builder
54
     * @return void
55
     */
56
    public function extend(Builder $builder): void
57
    {
58
        foreach ($this->extensions as $extension) {
59
            $this->{"add{$extension}"}($builder);
60
        }
61
    }
62
63
    /**
64
     * Add the `approve` extension to the builder.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Builder $builder
67
     * @return void
68
     */
69
    protected function addApprove(Builder $builder): void
70
    {
71
        $builder->macro('approve', function (Builder $builder) {
72
            $builder->withNotApproved();
73
74
            return $builder->update(['approved_at' => Date::now()]);
75
        });
76
    }
77
78
    /**
79
     * Add the `undoApprove` extension to the builder.
80
     *
81
     * @param \Illuminate\Database\Eloquent\Builder $builder
82
     * @return void
83
     */
84
    protected function addUndoApprove(Builder $builder): void
85
    {
86
        $builder->macro('undoApprove', function (Builder $builder) {
87
            return $builder->update(['approved_at' => null]);
88
        });
89
    }
90
91
    /**
92
     * Add the `withNotApproved` extension to the builder.
93
     *
94
     * @param \Illuminate\Database\Eloquent\Builder $builder
95
     * @return void
96
     */
97
    protected function addWithNotApproved(Builder $builder): void
98
    {
99
        $builder->macro('withNotApproved', function (Builder $builder) {
100
            return $builder->withoutGlobalScope($this);
101
        });
102
    }
103
104
    /**
105
     * Add the `withoutNotApproved` extension to the builder.
106
     *
107
     * @param \Illuminate\Database\Eloquent\Builder $builder
108
     * @return void
109
     */
110
    protected function addWithoutNotApproved(Builder $builder): void
111
    {
112
        $builder->macro('withoutNotApproved', function (Builder $builder) {
113
            return $builder->withoutGlobalScope($this)->whereNotNull('approved_at');
114
        });
115
    }
116
117
    /**
118
     * Add the `onlyNotApproved` extension to the builder.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $builder
121
     * @return void
122
     */
123
    protected function addOnlyNotApproved(Builder $builder): void
124
    {
125
        $builder->macro('onlyNotApproved', function (Builder $builder) {
126
            return $builder->withoutGlobalScope($this)->whereNull('approved_at');
127
        });
128
    }
129
}
130