Completed
Pull Request — master (#179)
by
unknown
01:34
created

WhereScope::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Translatable\Scopes;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Scope;
7
use Illuminate\Database\Eloquent\Builder;
8
9
class WhereScope implements Scope
10
{
11
    /**
12
     * Apply the scope to a given Eloquent query builder.
13
     *
14
     * @param  \Illuminate\Database\Eloquent\Builder $builder
15
     * @param  \Illuminate\Database\Eloquent\Model $model
16
     * @return void
17
     */
18
    public function apply(Builder $builder, Model $model)
19
    {
20
        $newQuery = $this->changeQuery($builder, $model);
21
22
        $builder->setQuery($newQuery);
23
    }
24
25
    /**
26
     * @param Builder $builder
27
     * @param Model $model
28
     * @return \Illuminate\Database\Query\Builder
29
     */
30
    private function changeQuery(Builder $builder, Model $model)
31
    {
32
        /** @var \Spatie\Translatable\HasTranslations $model */
33
        $translatableColumns = $model->getTranslatableAttributes();
34
        $query = $builder->getQuery();
35
        $wheres = $query->wheres;
36
        $locale = app()->getLocale();
37
38
        $newArr = [];
39
        foreach ($wheres as $key => $where) {
40 View Code Duplication
            if (method_exists($this, "iterateOn" . $where['type'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
41
                $newArr[] = $this->{"iterateOn" . $where['type']}($where, $translatableColumns, $locale);
42
            } else {
43
                $newArr[] = $where;
44
            }
45
        }
46
47
        if (!empty($newArr)) {
48
            $query->wheres = $newArr;
49
        }
50
51
        return $query;
52
    }
53
54
    /**
55
     * @param $where
56
     * @param $translatableColumns
57
     * @param $locale
58
     * @return mixed
59
     */
60
    private function iterateOnBasic($where, $translatableColumns, $locale)
61
    {
62
        if (
63
            array_key_exists('column', $where) &&
64
            in_array($where['column'], $translatableColumns)
65
        ) {
66
            $where['column'] = explode('->', $where['column'])[0] . "->{$locale}";
67
        }
68
69
        return $where;
70
    }
71
72
    /**
73
     * @param $where
74
     * @param $translatableColumns
75
     * @param $locale
76
     * @return mixed
77
     */
78
    private function iterateOnNested($where, $translatableColumns, $locale)
79
    {
80
        $nestedQuery = $where['query'];
81
        $nestedWheres = $nestedQuery->wheres;
82
        $newNestedArr = [];
83
        foreach ($nestedWheres as $nestedWhere) {
84
85 View Code Duplication
            if (method_exists($this, "iterateOn" . $nestedWhere['type'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
86
                $newNestedArr [] = $this->{"iterateOn" . $nestedWhere['type']}($nestedWhere, $translatableColumns, $locale);
87
            }
88
        }
89
90
        $nestedQuery->wheres = $newNestedArr;
91
        $where['query'] = $nestedQuery;
92
        return $where;
93
    }
94
}
95