ArchivedAtScope::addOnlyArchived()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
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 ArchivedAtScope implements Scope
22
{
23
    /**
24
     * All of the extensions to be added to the builder.
25
     *
26
     * @var array
27
     */
28
    protected $extensions = [
29
        'UndoArchive',
30
        'Archive',
31
        'WithArchived',
32
        'WithoutArchived',
33
        'OnlyArchived',
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, 'shouldApplyArchivedAtScope') && $model->shouldApplyArchivedAtScope()) {
46
            $builder->whereNull('archived_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 `undoArchive` extension to the builder.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Builder $builder
67
     * @return void
68
     */
69
    protected function addUndoArchive(Builder $builder): void
70
    {
71
        $builder->macro('undoArchive', function (Builder $builder) {
72
            $builder->withArchived();
73
74
            return $builder->update(['archived_at' => null]);
75
        });
76
    }
77
78
    /**
79
     * Add the `archive` extension to the builder.
80
     *
81
     * @param \Illuminate\Database\Eloquent\Builder $builder
82
     * @return void
83
     */
84
    protected function addArchive(Builder $builder): void
85
    {
86
        $builder->macro('archive', function (Builder $builder) {
87
            return $builder->update(['archived_at' => Date::now()]);
88
        });
89
    }
90
91
    /**
92
     * Add the `withArchived` extension to the builder.
93
     *
94
     * @param \Illuminate\Database\Eloquent\Builder $builder
95
     * @return void
96
     */
97
    protected function addWithArchived(Builder $builder): void
98
    {
99
        $builder->macro('withArchived', function (Builder $builder) {
100
            return $builder->withoutGlobalScope($this);
101
        });
102
    }
103
104
    /**
105
     * Add the `withoutArchived` extension to the builder.
106
     *
107
     * @param \Illuminate\Database\Eloquent\Builder $builder
108
     * @return void
109
     */
110
    protected function addWithoutArchived(Builder $builder): void
111
    {
112
        $builder->macro('withoutArchived', function (Builder $builder) {
113
            return $builder->withoutGlobalScope($this)->whereNull('archived_at');
114
        });
115
    }
116
117
    /**
118
     * Add the `onlyArchived` extension to the builder.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $builder
121
     * @return void
122
     */
123
    protected function addOnlyArchived(Builder $builder): void
124
    {
125
        $builder->macro('onlyArchived', function (Builder $builder) {
126
            return $builder->withoutGlobalScope($this)->whereNotNull('archived_at');
127
        });
128
    }
129
}
130