Passed
Pull Request — master (#4)
by
unknown
14:26
created

ActivatableScope::addWithDeactivated()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace niclasleonbock\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Scope;
8
9
class ActivatableScope implements Scope
10
{
11
12
    /**
13
     * All of the extensions to be added to the builder.
14
     *
15
     * @var array
16
     */
17
    protected $extensions = ['WithDeactivated', 'WithoutDeactivated', 'OnlyDeactivated', 'Deactivate', 'Activate'];
18
19
    /**
20
     * Apply the scope to a given Eloquent query builder.
21
     *
22
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
23
     * @return void
24
     */
25
    public function apply(Builder $builder, Model $model)
26
    {
27
        $builder->whereNotNull($model->getQualifiedActivatedAtColumn());
28
    }
29
30
    public function extend(Builder $builder)
31
    {
32
        foreach ( $this->extensions as $extension ) {
33
            $this->{"add{$extension}"}($builder);
34
        }
35
    }
36
37
    public function addWithDeactivated(Builder $builder)
38
    {
39
        $builder->macro('withDeactivated', function (Builder $builder, $withDeactivated = true) {
40
            if ( !$withDeactivated ) {
41
                return $builder->withoutDeactivated();
42
            }
43
44
            return $builder->withoutGlobalScope($this);
45
        });
46
    }
47
48 View Code Duplication
    public function addWithoutDeactivated(Builder $builder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $builder->macro('withoutDeactivated', function (Builder $builder) {
51
            $model = $builder->getModel();
52
53
            $builder->withoutGlobalScope($this)->whereNotNull(
54
                $model->getQualifiedActivatedAtColumn()
55
            );
56
57
            return $builder;
58
        });
59
    }
60
61 View Code Duplication
    public function addOnlyDeactivated(Builder $builder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $builder->macro('onlyDeactivated', function (Builder $builder) {
64
            $model = $builder->getModel();
65
66
            $builder->withoutGlobalScope($this)->whereNull(
67
                $model->getQualifiedActivatedAtColumn()
68
            );
69
70
            return $builder;
71
        });
72
    }
73
74
    public function addDeactivate(Builder $builder)
75
    {
76
        $builder->macro('deactivate', function (Builder $builder) {
77
            $model = $builder->getModel();
78
79
            $builder->withDeactivated();
80
81
            return $builder->update([$model->getActivatedAtColumn() => null]);
82
        });
83
    }
84
85
    public function addActivate(Builder $builder)
86
    {
87
        $builder->macro('activate', function (Builder $builder) {
0 ignored issues
show
Unused Code introduced by
The parameter $builder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
            $model = $this->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not seem to exist on object<niclasleonbock\Eloquent\ActivatableScope>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
90
            $query = $model->newQuery()->where($model->getKeyName(), $model->getKey());
91
92
            $model->{$model->getActivatedAtColumn()} = $time = $model->freshTimestamp();
93
94
            $query->update([$model->getActivatedAtColumn() => $model->fromDateTime($time)]);
95
        });
96
    }
97
98
    /**
99
     * Remove the scope from the given Eloquent query builder.
100
     *
101
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
102
     * @return void
103
     */
104
    public function remove(Builder $builder)
105
    {
106
        $column = $builder->getModel()->getQualifiedActivatedAtColumn();
107
        $query = $builder->getQuery();
108
109
        foreach ((array) $query->wheres as $key => $where) {
110
            if ($this->isActivatedConstraint($where, $column)) {
111
                unset($query->wheres[$key]);
112
113
                $query->wheres = array_values($query->wheres);
114
            }
115
        }
116
    }
117
118
    /**
119
     * Determine if the given where clause is an activated constraint.
120
     *
121
     * @param  array   $where
122
     * @param  string  $column
123
     * @return bool
124
     */
125
    protected function isActivatedConstraint(array $where, $column)
126
    {
127
        return $where['type'] == 'NotNull' && $where['column'] == $column;
128
    }
129
}
130