Completed
Push — master ( 12d899...a33b7a )
by Avtandil
04:40
created

LocalizableScope::getLocalizableColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
1
<?php
2
/*
3
 * This file is part of the Laravel MultiLang package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\LaravelMultiLang\Models;
12
13
use Illuminate\Database\Eloquent\Builder;
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Database\Eloquent\ScopeInterface;
16
17
18
class LocalizableScope implements ScopeInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Illuminate\Database\Eloquent\ScopeInterface has been deprecated with message: since version 5.2. Use Illuminate\Database\Eloquent\Scope.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    /**
21
     * Apply the scope to a given Eloquent query builder.
22
     *
23
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
24
     * @param  \Illuminate\Database\Eloquent\Model  $model
25
     * @return void
26
     */
27
    public function apply(Builder $builder, Model $model)
28
    {
29
30
        if (!$this->queryHasLocalizableColumn($builder)) {
31
            $builder->where($model->getQualifiedLocalizableColumn(), '=', app()->getLocale());
32
        }
33
    }
34
35
    protected function queryHasLocalizableColumn(Builder $builder)
36
    {
37
        $wheres = $builder->getQuery()->wheres;
38
        $column = $this->getLocalizableColumn($builder);
39
        foreach($wheres as $where) {
40
            if ($where['column'] == $column) {
41
                return true;
42
            }
43
        }
44
        return false;
45
    }
46
47
    /**
48
     * Get the "deleted at" column for the builder.
49
     *
50
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
51
     * @return string
52
     */
53
    protected function getLocalizableColumn(Builder $builder)
54
    {
55
        if (count($builder->getQuery()->joins) > 0) {
56
            return $builder->getModel()->getQualifiedLocalizableColumn();
57
        } else {
58
            return $builder->getModel()->getLocalizableColumn();
59
        }
60
    }
61
62
63
    /**
64
     * Remove the scope from the given Eloquent query builder.
65
     *
66
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
67
     * @param  \Illuminate\Database\Eloquent\Model  $model
68
     * @return void
69
     */
70
    public function remove(Builder $builder, Model $model)
71
    {
72
        $column = $model->getQualifiedDeletedAtColumn();
0 ignored issues
show
Bug introduced by
The method getQualifiedDeletedAtColumn() does not exist on Illuminate\Database\Eloquent\Model. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
73
74
        $query = $builder->getQuery();
75
76
        foreach ((array) $query->wheres as $key => $where)
77
        {
78
            // If the where clause is a soft delete date constraint, we will remove it from
79
            // the query and reset the keys on the wheres. This allows this developer to
80
            // include deleted model in a relationship result set that is lazy loaded.
81
            if ($this->isSoftDeleteConstraint($where, $column))
0 ignored issues
show
Bug introduced by
The method isSoftDeleteConstraint() does not seem to exist on object<Longman\LaravelMu...odels\LocalizableScope>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            {
83
                unset($query->wheres[$key]);
84
85
                $query->wheres = array_values($query->wheres);
86
            }
87
        }
88
    }
89
}
90