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) |
|
|
|
|
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) |
|
|
|
|
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) { |
|
|
|
|
88
|
|
|
$model = $this->getModel(); |
|
|
|
|
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
|
|
|
|
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.