Completed
Push — master ( 84015a...c60f49 )
by Maxime
115:43 queued 111:38
created

StatusScope   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.61%

Importance

Changes 6
Bugs 1 Features 3
Metric Value
wmc 6
c 6
b 1
f 3
lcom 1
cbo 2
dl 0
loc 78
ccs 19
cts 23
cp 0.8261
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 9 2
A extend() 0 8 2
A addWithOffline() 0 7 1
A addOnlyOffline() 0 12 1
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 344
    public function apply(Builder $builder, Model $model)
26
    {
27 344
        if (!UserUtils::hasDisplayAllStatus())
28 344
        {
29 344
            $builder->where($model->getQualifiedStatusColumn(), true);
30 344
        }
31 344
        $this->extend($builder);
32
33 344
    }
34
35
    /**
36
     * Extend the query builder with the needed functions.
37
     *
38
     * @param  \Illuminate\Database\Eloquent\Builder $builder
39
     * @return void
40
     */
41 360
    public function extend(Builder $builder)
42
    {
43 360
        foreach ($this->extensions as $extension)
44
        {
45 360
            $this->{"add{$extension}"}($builder);
46 360
        }
47
48 360
    }
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 360
    protected function addWithOffline(Builder $builder)
58
    {
59
        $builder->macro('withOffline', function(Builder $builder)
60
        {
61 4
            return $builder->withoutGlobalScope($this);
62 360
        });
63 360
    }
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
    protected function addOnlyOffline(Builder $builder)
73
    {
74 360
        $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 360
        });
83 360
    }
84
85
}