Completed
Pull Request — master (#67)
by
unknown
02:40
created

CommentAtScope   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
eloc 22
c 1
b 0
f 1
dl 0
loc 106
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addWithoutNotComment() 0 4 1
A extend() 0 4 2
A addUndoComment() 0 4 1
A addComment() 0 6 1
A addOnlyNotComment() 0 4 1
A apply() 0 4 3
A addWithNotComment() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Laravel Eloquent Flag.
5
 *
6
 * (c) Sivan Wolberg <[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 CommentAtScope implements Scope
22
{
23
    /**
24
     * All of the extensions to be added to the builder.
25
     *
26
     * @var array
27
     */
28
    protected $extensions = [
29
        'Comment',
30
        'UndoComment',
31
        'WithNotComment',
32
        'WithoutNotComment',
33
        'OnlyNotComment',
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, 'shouldApplyCommentAtScope') && $model->shouldApplyVerifiedAtScope()) {
46
            $builder->whereNotNull('commentable_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 `comment` extension to the builder.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Builder $builder
67
     * @return void
68
     */
69
    protected function addComment(Builder $builder): void
70
    {
71
        $builder->macro('comment', function (Builder $builder) {
72
            $builder->withNotVerified();
73
74
            return $builder->update(['commentable_at' => Date::now()]);
75
        });
76
    }
77
78
    /**
79
     * Add the `undoComment` extension to the builder.
80
     *
81
     * @param \Illuminate\Database\Eloquent\Builder $builder
82
     * @return void
83
     */
84
    protected function addUndoComment(Builder $builder): void
85
    {
86
        $builder->macro('undoComment', function (Builder $builder) {
87
            return $builder->update(['commentable_at' => null]);
88
        });
89
    }
90
91
    /**
92
     * Add the `withNotComment` extension to the builder.
93
     *
94
     * @param \Illuminate\Database\Eloquent\Builder $builder
95
     * @return void
96
     */
97
    protected function addWithNotComment(Builder $builder): void
98
    {
99
        $builder->macro('withNotComment', function (Builder $builder) {
100
            return $builder->withoutGlobalScope($this);
101
        });
102
    }
103
104
    /**
105
     * Add the `withoutNotComment` extension to the builder.
106
     *
107
     * @param \Illuminate\Database\Eloquent\Builder $builder
108
     * @return void
109
     */
110
    protected function addWithoutNotComment(Builder $builder): void
111
    {
112
        $builder->macro('withoutNotComment', function (Builder $builder) {
113
            return $builder->withoutGlobalScope($this)->whereNotNull('commentable_at');
114
        });
115
    }
116
117
    /**
118
     * Add the `onlyNotComment` extension to the builder.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $builder
121
     * @return void
122
     */
123
    protected function addOnlyNotComment(Builder $builder): void
124
    {
125
        $builder->macro('onlyNotComment', function (Builder $builder) {
126
            return $builder->withoutGlobalScope($this)->whereNull('commentable_at');
127
        });
128
    }
129
}
130