Passed
Push — master ( ff8110...11ccd8 )
by Quentin
04:48 queued 10s
created

HasTranslation::getTranslationModelNameDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use Astrotomic\Translatable\Translatable;
6
use Illuminate\Database\Query\JoinClause;
7
8
trait HasTranslation
9
{
10
    use Translatable;
11
12 19
    public function getTranslationModelNameDefault()
13
    {
14 19
        return config('twill.namespace') . "\Models\Translations\\" . class_basename($this) . 'Translation';
15
    }
16
17
    public function scopeWithActiveTranslations($query, $locale = null)
18
    {
19
        if (method_exists($query->getModel(), 'translations')) {
20
            $locale = $locale == null ? app()->getLocale() : $locale;
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

20
            $locale = $locale == null ? app()->/** @scrutinizer ignore-call */ getLocale() : $locale;
Loading history...
21
22
            $query->whereHas('translations', function ($query) use ($locale) {
23
                $query->whereActive(true);
24
                $query->whereLocale($locale);
25
26
                if (config('translatable.use_property_fallback', false)) {
27
                    $query->orWhere('locale', config('translatable.fallback_locale'));
28
                }
29
            });
30
31
            return $query->with(['translations' => function ($query) use ($locale) {
32
                $query->whereActive(true);
33
                $query->whereLocale($locale);
34
35
                if (config('translatable.use_property_fallback', false)) {
36
                    $query->orWhere('locale', config('translatable.fallback_locale'));
37
                }
38
            }]);
39
        }
40
    }
41
42
    public function scopeOrderByTranslation($query, $orderField, $orderType = 'ASC', $locale = null)
43
    {
44
        $translationTable = $this->getTranslationsTable();
45
        $localeKey = $this->getLocaleKey();
46
        $table = $this->getTable();
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        /** @scrutinizer ignore-call */ 
47
        $table = $this->getTable();
Loading history...
47
        $keyName = $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
        /** @scrutinizer ignore-call */ 
48
        $keyName = $this->getKeyName();
Loading history...
48
        $locale = $locale == null ? app()->getLocale() : $locale;
49
50
        return $query
51
            ->join($translationTable, function (JoinClause $join) use ($translationTable, $localeKey, $table, $keyName) {
52
                $join
53
                    ->on($translationTable.'.'.$this->getRelationKey(), '=', $table.'.'.$keyName)
0 ignored issues
show
Deprecated Code introduced by
The function A17\Twill\Models\Behavio...ation::getRelationKey() has been deprecated. ( Ignorable by Annotation )

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

53
                    ->on($translationTable.'.'./** @scrutinizer ignore-deprecated */ $this->getRelationKey(), '=', $table.'.'.$keyName)
Loading history...
54
                    ->where($translationTable.'.'.$localeKey, $this->locale());
55
            })
56
            ->where($translationTable.'.'.$this->getLocaleKey(), $locale)
57
            ->orderBy($translationTable.'.'.$orderField, $orderType)
58
            ->select($table.'.*')
59
            ->with('translations');
60
    }
61
62
    public function scopeOrderByRawByTranslation($query, $orderRawString, $groupByField, $locale = null)
63
    {
64
        $translationTable = $this->getTranslationsTable();
65
        $table = $this->getTable();
66
        $locale = $locale == null ? app()->getLocale() : $locale;
67
68
        return $query->join("{$translationTable} as t", "t.{$this->getRelationKey()}", "=", "{$table}.id")
0 ignored issues
show
Deprecated Code introduced by
The function A17\Twill\Models\Behavio...ation::getRelationKey() has been deprecated. ( Ignorable by Annotation )

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

68
        return $query->join("{$translationTable} as t", "t.{/** @scrutinizer ignore-deprecated */ $this->getRelationKey()}", "=", "{$table}.id")
Loading history...
69
            ->where($this->getLocaleKey(), $locale)
70
            ->groupBy("{$table}.id")
71
            ->groupBy("t.{$groupByField}")
72
            ->select("{$table}.*")
73
            ->orderByRaw($orderRawString)
74
            ->with('translations');
75
    }
76
77
    public function hasActiveTranslation($locale = null)
78
    {
79
        $locale = $locale ?: $this->locale();
80
81
        $translations = $this->memoizedTranslations ?? ($this->memoizedTranslations = $this->translations()->get());
0 ignored issues
show
Bug Best Practice introduced by
The property memoizedTranslations does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
82
83
        foreach ($translations as $translation) {
84
            if ($translation->getAttribute($this->getLocaleKey()) == $locale && $translation->getAttribute('active')) {
85
                return true;
86
            }
87
        }
88
89
        return false;
90
    }
91
92 4
    public function getActiveLanguages()
93
    {
94
        return $this->translations->map(function ($translation) {
95
            return [
96 4
                'shortlabel' => strtoupper($translation->locale),
97 4
                'label' => getLanguageLabelFromLocaleCode($translation->locale),
98 4
                'value' => $translation->locale,
99 4
                'published' => $translation->active ?? false,
100
            ];
101
        })->sortBy(function ($translation) {
102 4
            $localesOrdered = config('translatable.locales');
103 4
            return array_search($translation['value'], $localesOrdered);
104 4
        })->values();
105
    }
106
107
    public function translatedAttribute($key)
108
    {
109
        return $this->translations->mapWithKeys(function ($translation) use ($key) {
110
            return [$translation->locale => $this->translate($translation->locale)->$key];
111
        });
112
    }
113
114
}
115