TranslatableScope   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 64
ccs 0
cts 23
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 19 2
A extend() 0 4 2
A addWithoutTranslation() 0 4 1
1
<?php namespace Distilleries\Expendable\Scopes;
2
3
use Illuminate\Database\Eloquent\Scope;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Database\Eloquent\Model;
6
7
class TranslatableScope implements Scope {
8
9
    /**
10
     * All of the extensions to be added to the builder.
11
     *
12
     * @var array
13
     */
14
    protected $extensions = ['WithoutTranslation'];
15
16
17
    /**
18
     * Apply the scope to a given Eloquent query builder.
19
     *
20
     * @param  \Illuminate\Database\Eloquent\Builder $builder
21
     * @param  \Illuminate\Database\Eloquent\Model $model
22
     * @return void
23
     */
24
    public function apply(Builder $builder, Model $model)
25
    {
26
        //Check if is an override
27
        //
28
        $iso = config()->get('local_override');
29
30
        if (empty($iso)) {
31
            $iso = app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            $iso = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
32
        }
33
34
        $builder->whereExists(function($query) use ($model, $iso) {
35
            $query->select(\DB::raw(1))
36
                ->from($model->getQualifiedTable())
37
                ->whereRaw($model->getTable().'.'.$model->getKeyName().' = '.$model->getQualifiedIdElementColumn())
0 ignored issues
show
Bug introduced by
Are you sure $model->getQualifiedIdElementColumn() of type Illuminate\Database\Eloquent\Builder|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
                ->whereRaw($model->getTable().'.'.$model->getKeyName().' = './** @scrutinizer ignore-type */ $model->getQualifiedIdElementColumn())
Loading history...
38
                ->where($model->getQualifiedIsoColumn(), $iso)
39
                ->where($model->getQualifiedModelColumn(), $model->getTable());
40
        });
41
42
        $this->extend($builder);
43
44
    }
45
46
    /**
47
     * Extend the query builder with the needed functions.
48
     *
49
     * @param  \Illuminate\Database\Eloquent\Builder $builder
50
     * @return void
51
     */
52
    public function extend(Builder $builder)
53
    {
54
        foreach ($this->extensions as $extension) {
55
            $this->{"add{$extension}"}($builder);
56
        }
57
58
    }
59
60
61
    /**
62
     * Add the with-trashed extension to the builder.
63
     *
64
     * @param  \Illuminate\Database\Eloquent\Builder $builder
65
     * @return void
66
     */
67
    protected function addWithoutTranslation(Builder $builder)
68
    {
69
        $builder->macro('withoutTranslation', function(Builder $builder) {
70
            return $builder->withoutGlobalScope($this);
71
        });
72
    }
73
}