LocalizableBuilder::joinWithTranslation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Locale\Database;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Locale\Models\Localizable;
7
8
class LocalizableBuilder extends Builder
9
{
10
    /**
11
     * @var bool
12
     */
13
    protected $translationJoined = false;
14
15
    /**
16
     * Add an "order by" clause to the query.
17
     *
18
     * @param  string $column
19
     * @param  string $direction
20
     * @return Builder|LocalizableBuilder
21
     */
22
    public function orderBy($column, $direction = 'asc')
23
    {
24
        /** @var Localizable $localizableModel */
25
        $localizableModel = $this->model;
26
27
        if ($localizableModel->isLocalizableAttribute($column)) {
28
            $this->joinWithTranslation();
29
        }
30
31
        return parent::orderBy($column, $direction);
0 ignored issues
show
introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. 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
        return parent::/** @scrutinizer ignore-call */ orderBy($column, $direction);
Loading history...
32
    }
33
34
    /**
35
     * @since 1.0.0
36
     */
37
    public function joinWithTranslation()
38
    {
39
        if (!$this->translationJoined) {
40
            /** @var Localizable $localizableModel */
41
            $localizableModel = $this->model;
42
43
            $localeTable = config("locale.model");
44
            $modelTable = $localizableModel->getTable();
45
            $modelKeyName = $localizableModel->getKeyName();
46
            $joiningTable = $localizableModel->joiningLocaleTable($localeTable, $modelTable);
47
            $modelForeignKey = $localizableModel->getModelForeignKey();
48
49
            $this->join($joiningTable, "{$modelTable}.{$modelKeyName}", "=", "{$joiningTable}.{$modelForeignKey}");
0 ignored issues
show
Bug introduced by
The method join() does not exist on Locale\Database\LocalizableBuilder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

49
            $this->/** @scrutinizer ignore-call */ 
50
                   join($joiningTable, "{$modelTable}.{$modelKeyName}", "=", "{$joiningTable}.{$modelForeignKey}");
Loading history...
50
        }
51
    }
52
}
53