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

ApprovedAtScope::extend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
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
        $builder->whereNotNull('approved_at');
46
    }
47
48
    /**
49
     * Extend the query builder with the needed functions.
50
     *
51
     * @param \Illuminate\Database\Eloquent\Builder $builder
52
     * @return void
53
     */
54
    public function extend(Builder $builder): void
55
    {
56
        foreach ($this->extensions as $extension) {
57
            $this->{"add{$extension}"}($builder);
58
        }
59
    }
60
61
    /**
62
     * Add the `approve` extension to the builder.
63
     *
64
     * @param \Illuminate\Database\Eloquent\Builder $builder
65
     * @return void
66
     */
67
    protected function addApprove(Builder $builder): void
68
    {
69
        $builder->macro('approve', function (Builder $builder) {
70
            $builder->withNotApproved();
71
72
            return $builder->update(['approved_at' => Date::now()]);
73
        });
74
    }
75
76
    /**
77
     * Add the `undoApprove` extension to the builder.
78
     *
79
     * @param \Illuminate\Database\Eloquent\Builder $builder
80
     * @return void
81
     */
82
    protected function addUndoApprove(Builder $builder): void
83
    {
84
        $builder->macro('undoApprove', function (Builder $builder) {
85
            return $builder->update(['approved_at' => null]);
86
        });
87
    }
88
89
    /**
90
     * Add the `withNotApproved` extension to the builder.
91
     *
92
     * @param \Illuminate\Database\Eloquent\Builder $builder
93
     * @return void
94
     */
95
    protected function addWithNotApproved(Builder $builder): void
96
    {
97
        $builder->macro('withNotApproved', function (Builder $builder) {
98
            return $builder->withoutGlobalScope($this);
99
        });
100
    }
101
102
    /**
103
     * Add the `withoutNotApproved` extension to the builder.
104
     *
105
     * @param \Illuminate\Database\Eloquent\Builder $builder
106
     * @return void
107
     */
108
    protected function addWithoutNotApproved(Builder $builder): void
109
    {
110
        $builder->macro('withoutNotApproved', function (Builder $builder) {
111
            return $builder->withoutGlobalScope($this)->whereNotNull('approved_at');
112
        });
113
    }
114
115
    /**
116
     * Add the `onlyNotApproved` extension to the builder.
117
     *
118
     * @param \Illuminate\Database\Eloquent\Builder $builder
119
     * @return void
120
     */
121
    protected function addOnlyNotApproved(Builder $builder): void
122
    {
123
        $builder->macro('onlyNotApproved', function (Builder $builder) {
124
            return $builder->withoutGlobalScope($this)->whereNull('approved_at');
125
        });
126
    }
127
}
128