DraftedAtScope::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
use Illuminate\Support\Facades\Date;
20
21
final class DraftedAtScope implements Scope
22
{
23
    /**
24
     * All of the extensions to be added to the builder.
25
     *
26
     * @var array
27
     */
28
    protected $extensions = [
29
        'UndoDraft',
30
        'Draft',
31
        'WithDrafted',
32
        'WithoutDrafted',
33
        'OnlyDrafted',
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, 'shouldApplyDraftedAtScope') && $model->shouldApplyDraftedAtScope()) {
46
            $builder->whereNull('drafted_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 `undoDraft` extension to the builder.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Builder $builder
67
     * @return void
68
     */
69
    protected function addUndoDraft(Builder $builder): void
70
    {
71
        $builder->macro('undoDraft', function (Builder $builder) {
72
            $builder->withDrafted();
73
74
            return $builder->update(['drafted_at' => null]);
75
        });
76
    }
77
78
    /**
79
     * Add the `draft` extension to the builder.
80
     *
81
     * @param \Illuminate\Database\Eloquent\Builder $builder
82
     * @return void
83
     */
84
    protected function addDraft(Builder $builder): void
85
    {
86
        $builder->macro('draft', function (Builder $builder) {
87
            return $builder->update(['drafted_at' => Date::now()]);
88
        });
89
    }
90
91
    /**
92
     * Add the `withDrafted` extension to the builder.
93
     *
94
     * @param \Illuminate\Database\Eloquent\Builder $builder
95
     * @return void
96
     */
97
    protected function addWithDrafted(Builder $builder): void
98
    {
99
        $builder->macro('withDrafted', function (Builder $builder) {
100
            return $builder->withoutGlobalScope($this);
101
        });
102
    }
103
104
    /**
105
     * Add the `withoutDrafted` extension to the builder.
106
     *
107
     * @param \Illuminate\Database\Eloquent\Builder $builder
108
     * @return void
109
     */
110
    protected function addWithoutDrafted(Builder $builder): void
111
    {
112
        $builder->macro('withoutDrafted', function (Builder $builder) {
113
            return $builder->withoutGlobalScope($this)->whereNull('drafted_at');
114
        });
115
    }
116
117
    /**
118
     * Add the `onlyDrafted` extension to the builder.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $builder
121
     * @return void
122
     */
123
    protected function addOnlyDrafted(Builder $builder): void
124
    {
125
        $builder->macro('onlyDrafted', function (Builder $builder) {
126
            return $builder->withoutGlobalScope($this)->whereNotNull('drafted_at');
127
        });
128
    }
129
}
130