CancellingScope   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A extend() 0 4 2
A addWithCancelled() 0 4 1
A addWithoutCancelled() 0 10 1
A addKeep() 0 6 1
A apply() 0 4 2
A addOnlyCancelled() 0 10 1
A getCancelledAtColumn() 0 7 2
1
<?php
2
3
namespace Signifly\Cancellation\Scopes;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Scope;
8
9
class CancellingScope implements Scope
10
{
11
    /**
12
     * All of the extensions to be added to the builder.
13
     *
14
     * @var array
15
     */
16
    protected $extensions = ['Keep', 'WithCancelled', 'WithoutCancelled', 'OnlyCancelled'];
17
18
    /**
19
     * Apply the scope to a given Eloquent query builder.
20
     *
21
     * @param \Illuminate\Database\Eloquent\Builder $builder
22
     * @param \Illuminate\Database\Eloquent\Model   $model
23
     *
24
     * @return void
25
     */
26
    public function apply(Builder $builder, Model $model)
27
    {
28
        if (config('laravel-cancellation.exclude') === true) {
29
            $builder->whereNull($model->getQualifiedCancelledAtColumn());
30
        }
31
    }
32
33
    /**
34
     * Extend the query builder with the needed functions.
35
     *
36
     * @param \Illuminate\Database\Eloquent\Builder $builder
37
     *
38
     * @return void
39
     */
40
    public function extend(Builder $builder)
41
    {
42
        foreach ($this->extensions as $extension) {
43
            $this->{"add{$extension}"}($builder);
44
        }
45
    }
46
47
    /**
48
     * Get the "cancelled at" column for the builder.
49
     *
50
     * @param \Illuminate\Database\Eloquent\Builder $builder
51
     *
52
     * @return string
53
     */
54
    protected function getCancelledAtColumn(Builder $builder)
55
    {
56
        if (count($builder->getQuery()->joins) > 0) {
57
            return $builder->getModel()->getQualifiedCancelledAtColumn();
58
        }
59
60
        return $builder->getModel()->getCancelledAtColumn();
61
    }
62
63
    /**
64
     * Add the keep extension to the builder.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Builder $builder
67
     *
68
     * @return void
69
     */
70
    protected function addKeep(Builder $builder)
71
    {
72
        $builder->macro('keep', function (Builder $builder) {
73
            $builder->withCancelled();
74
75
            return $builder->update([$builder->getModel()->getCancelledAtColumn() => null]);
76
        });
77
    }
78
79
    /**
80
     * Add the with-cancelled extension to the builder.
81
     *
82
     * @param \Illuminate\Database\Eloquent\Builder $builder
83
     *
84
     * @return void
85
     */
86
    protected function addWithCancelled(Builder $builder)
87
    {
88
        $builder->macro('withCancelled', function (Builder $builder) {
89
            return $builder->withoutGlobalScope($this);
90
        });
91
    }
92
93
    /**
94
     * Add the without-cancelled extension to the builder.
95
     *
96
     * @param \Illuminate\Database\Eloquent\Builder $builder
97
     *
98
     * @return void
99
     */
100
    protected function addWithoutCancelled(Builder $builder)
101
    {
102
        $builder->macro('withoutCancelled', function (Builder $builder) {
103
            $model = $builder->getModel();
104
105
            $builder->withoutGlobalScope($this)->whereNull(
106
                $model->getQualifiedCancelledAtColumn()
107
            );
108
109
            return $builder;
110
        });
111
    }
112
113
    /**
114
     * Add the only-cancelled extension to the builder.
115
     *
116
     * @param \Illuminate\Database\Eloquent\Builder $builder
117
     *
118
     * @return void
119
     */
120
    protected function addOnlyCancelled(Builder $builder)
121
    {
122
        $builder->macro('onlyCancelled', function (Builder $builder) {
123
            $model = $builder->getModel();
124
125
            $builder->withoutGlobalScope($this)->whereNotNull(
126
                $model->getQualifiedCancelledAtColumn()
127
            );
128
129
            return $builder;
130
        });
131
    }
132
}
133