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

VerifiedAtScope::addWithNotVerified()   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
use Illuminate\Support\Facades\Date;
20
21
final class VerifiedAtScope implements Scope
22
{
23
    /**
24
     * All of the extensions to be added to the builder.
25
     *
26
     * @var array
27
     */
28
    protected $extensions = [
29
        'Verify',
30
        'UndoVerify',
31
        'WithNotVerified',
32
        'WithoutNotVerified',
33
        'OnlyNotVerified',
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, 'shouldApplyVerifiedAtScope') && !$model->shouldApplyVerifiedAtScope()) {
46
            return;
47
        }
48
49
        $builder->whereNotNull('verified_at');
50
    }
51
52
    /**
53
     * Extend the query builder with the needed functions.
54
     *
55
     * @param \Illuminate\Database\Eloquent\Builder $builder
56
     * @return void
57
     */
58
    public function extend(Builder $builder): void
59
    {
60
        foreach ($this->extensions as $extension) {
61
            $this->{"add{$extension}"}($builder);
62
        }
63
    }
64
65
    /**
66
     * Add the `verify` extension to the builder.
67
     *
68
     * @param \Illuminate\Database\Eloquent\Builder $builder
69
     * @return void
70
     */
71
    protected function addVerify(Builder $builder): void
72
    {
73
        $builder->macro('verify', function (Builder $builder) {
74
            $builder->withNotVerified();
75
76
            return $builder->update(['verified_at' => Date::now()]);
77
        });
78
    }
79
80
    /**
81
     * Add the `undoVerify` extension to the builder.
82
     *
83
     * @param \Illuminate\Database\Eloquent\Builder $builder
84
     * @return void
85
     */
86
    protected function addUndoVerify(Builder $builder): void
87
    {
88
        $builder->macro('undoVerify', function (Builder $builder) {
89
            return $builder->update(['verified_at' => null]);
90
        });
91
    }
92
93
    /**
94
     * Add the `withNotVerified` extension to the builder.
95
     *
96
     * @param \Illuminate\Database\Eloquent\Builder $builder
97
     * @return void
98
     */
99
    protected function addWithNotVerified(Builder $builder): void
100
    {
101
        $builder->macro('withNotVerified', function (Builder $builder) {
102
            return $builder->withoutGlobalScope($this);
103
        });
104
    }
105
106
    /**
107
     * Add the `withoutNotVerified` extension to the builder.
108
     *
109
     * @param \Illuminate\Database\Eloquent\Builder $builder
110
     * @return void
111
     */
112
    protected function addWithoutNotVerified(Builder $builder): void
113
    {
114
        $builder->macro('withoutNotVerified', function (Builder $builder) {
115
            return $builder->withoutGlobalScope($this)->whereNotNull('verified_at');
116
        });
117
    }
118
119
    /**
120
     * Add the `onlyNotVerified` extension to the builder.
121
     *
122
     * @param \Illuminate\Database\Eloquent\Builder $builder
123
     * @return void
124
     */
125
    protected function addOnlyNotVerified(Builder $builder): void
126
    {
127
        $builder->macro('onlyNotVerified', function (Builder $builder) {
128
            return $builder->withoutGlobalScope($this)->whereNull('verified_at');
129
        });
130
    }
131
}
132