RivetTypeScope::apply()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Luminark\Rivet\Models\Scopes;
4
5
use Illuminate\Database\Eloquent\ScopeInterface;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
9
class RivetTypeScope implements ScopeInterface
10
{
11
    protected $type;
12
13
    public function __construct($type)
14
    {
15
        $this->type = $type;
16
    }
17
18
    public function apply(Builder $builder, Model $model)
19
    {
20
        $builder->where('type', $this->type);
21
    }
22
23
    public function remove(Builder $builder, Model $model)
24
    {
25
        $query = $builder->getQuery();
26
27
        foreach ((array) $query->wheres as $key => $where) {
28
            if ($key == 'type') {
29
                unset($query->wheres[$key]);
30
            }
31
        }
32
33
        $query->wheres = array_values($query->wheres);
34
    }
35
}
36