1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\Publishable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Scope; |
8
|
|
|
|
9
|
|
|
class PublishedScope implements Scope |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* All of the extensions to be added to the builder. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $extensions = ['WithDrafts', 'OnlyDrafts', 'OnlyPublished']; |
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
|
1 |
|
public function apply(Builder $builder, Model $model) |
27
|
|
|
{ |
28
|
1 |
|
$builder->whereNotNull($model->getQualifiedPublishedAtColumn()); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Extend the query builder with the needed functions. |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
1 |
|
public function extend(Builder $builder) |
39
|
|
|
{ |
40
|
1 |
|
foreach ($this->extensions as $extension) { |
41
|
1 |
|
$this->{"add{$extension}"}($builder); |
42
|
|
|
} |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add the with-drafts extension to the builder. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
1 |
|
protected function addWithDrafts(Builder $builder) |
53
|
|
|
{ |
54
|
|
|
$builder->macro('withDrafts', function (Builder $builder) { |
55
|
1 |
|
return $builder->withoutGlobalScope($this); |
56
|
1 |
|
}); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add the only-published extension to the builder. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
protected function addOnlyPublished(Builder $builder) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$builder->macro('onlyPublished', function (Builder $builder) { |
69
|
1 |
|
$model = $builder->getModel(); |
70
|
|
|
|
71
|
1 |
|
$builder->withoutGlobalScope($this)->whereNotNull( |
72
|
1 |
|
$model->getQualifiedPublishedAtColumn() |
73
|
|
|
); |
74
|
|
|
|
75
|
1 |
|
return $builder; |
76
|
1 |
|
}); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add the only-drafts extension to the builder. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
1 |
View Code Duplication |
protected function addOnlyDrafts(Builder $builder) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$builder->macro('onlyDrafts', function (Builder $builder) { |
89
|
1 |
|
$model = $builder->getModel(); |
90
|
|
|
|
91
|
1 |
|
$builder->withoutGlobalScope($this)->whereNull( |
92
|
1 |
|
$model->getQualifiedPublishedAtColumn() |
93
|
|
|
); |
94
|
|
|
|
95
|
1 |
|
return $builder; |
96
|
1 |
|
}); |
97
|
1 |
|
} |
98
|
|
|
} |
99
|
|
|
|
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.