Passed
Branch master (ad686c)
by Maxime
05:36
created

StatusScope   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 6
eloc 14
dl 0
loc 74
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addWithOffline() 0 5 1
A addOnlyOffline() 0 10 1
A apply() 0 7 2
A extend() 0 5 2
1
<?php namespace Distilleries\Expendable\Scopes;
2
3
use Distilleries\Expendable\Helpers\UserUtils;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Database\Eloquent\Scope;
6
use Illuminate\Database\Eloquent\Model;
7
8
class StatusScope implements Scope{
9
10
    /**
11
     * All of the extensions to be added to the builder.
12
     *
13
     * @var array
14
     */
15
    protected $extensions = ['WithOffline', 'OnlyOffline'];
16
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
     * @return void
24
     */
25 162
    public function apply(Builder $builder, Model $model)
26
    {
27 162
        if (!UserUtils::hasDisplayAllStatus())
28
        {
29 48
            $builder->where($model->getQualifiedStatusColumn(), true);
30
        }
31 162
        $this->extend($builder);
32
33
    }
34
35
    /**
36
     * Extend the query builder with the needed functions.
37
     *
38
     * @param  \Illuminate\Database\Eloquent\Builder $builder
39
     * @return void
40
     */
41 204
    public function extend(Builder $builder)
42
    {
43 204
        foreach ($this->extensions as $extension)
44
        {
45 204
            $this->{"add{$extension}"}($builder);
46
        }
47
48
    }
49
50
51
    /**
52
     * Add the with-trashed extension to the builder.
53
     *
54
     * @param  \Illuminate\Database\Eloquent\Builder $builder
55
     * @return void
56
     */
57 204
    protected function addWithOffline(Builder $builder)
58
    {
59
        $builder->macro('withOffline', function(Builder $builder)
60
        {
61 2
            return $builder->withoutGlobalScope($this);
62 204
        });
63
    }
64
65
66
    /**
67
     * Add the only-trashed extension to the builder.
68
     *
69
     * @param  \Illuminate\Database\Eloquent\Builder $builder
70
     * @return void
71
     */
72 204
    protected function addOnlyOffline(Builder $builder)
73
    {
74
        $builder->macro('onlyOffline', function(Builder $builder)
75
        {
76
            $model = $builder->getModel();
77
78
            $builder->withoutGlobalScope($this)
79
                ->where($model->getQualifiedStatusColumn(), '=', false);
80
81
            return $builder;
82 204
        });
83
    }
84
85
}