Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — translatable-with-fallbacks ( 339f83...5b8b01 )
by Pedro
12:17
created

HasTranslations::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 2
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\CRUD\app\Models\Traits\SpatieTranslatable;
4
5
use Illuminate\Support\Arr;
6
use Spatie\Translatable\HasTranslations as OriginalHasTranslations;
7
use Spatie\Translatable\Translatable;
8
9
trait HasTranslations
10
{
11
    use OriginalHasTranslations;
12
13
    /**
14
     * @var bool
15
     */
16
    public $locale = false;
17
18
    /**
19
     * @var bool
20
     */
21
    public $useFallbackLocale = true;
22
23
    /*
24
    |--------------------------------------------------------------------------
25
    |                 SPATIE/LARAVEL-TRANSLATABLE OVERWRITES
26
    |--------------------------------------------------------------------------
27
    */
28
29
    /**
30
     * Use the forced locale if present.
31
     *
32
     * @param  string  $key
33
     * @return mixed
34
     */
35
    public function getAttributeValue($key)
36
    {
37
        if (! $this->isTranslatableAttribute($key)) {
38
            return parent::getAttributeValue($key);
39
        }
40
41
        $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'), $this->useFallbackLocale);
0 ignored issues
show
Bug introduced by
It seems like $this->locale ?: config('app.locale') can also be of type true; however, parameter $locale of Backpack\CRUD\app\Models...tions::getTranslation() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

41
        $translation = $this->getTranslation($key, /** @scrutinizer ignore-type */ $this->locale ?: config('app.locale'), $this->useFallbackLocale);
Loading history...
42
43
        // if it's a fake field, json_encode it
44
        if (is_array($translation)) {
45
            return json_encode($translation, JSON_UNESCAPED_UNICODE);
46
        }
47
48
        return $translation;
49
    }
50
51
    public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true)
52
    {
53
        $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale);
54
55
        $translations = $this->getTranslations($key);
56
57
        $translation = $translations[$locale] ?? '';
58
59
        if ($this->hasGetMutator($key)) {
0 ignored issues
show
Bug introduced by
The method hasGetMutator() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. 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

59
        if ($this->/** @scrutinizer ignore-call */ hasGetMutator($key)) {
Loading history...
60
            return $this->mutateAttribute($key, $translation);
0 ignored issues
show
Bug introduced by
The method mutateAttribute() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. 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

60
            return $this->/** @scrutinizer ignore-call */ mutateAttribute($key, $translation);
Loading history...
61
        }
62
63
        return $translation;
64
    }
65
66
    private function getFallbackFromUrl()
67
    {
68
        $fallback = app('crud')->getRequest()->get('_use_fallback');
69
        if (isset($fallback) && in_array($fallback, array_keys(app('crud')->getModel()->getAvailableLocales()))) {
70
            return $fallback;
71
        }
72
73
        return null;
74
    }
75
76
    /*
77
    |--------------------------------------------------------------------------
78
    |                            ELOQUENT OVERWRITES
79
    |--------------------------------------------------------------------------
80
    */
81
82
    /**
83
     * Create translated items as json.
84
     *
85
     * @param  array  $attributes
86
     * @return static
87
     */
88
    public static function create(array $attributes = [])
89
    {
90
        $locale = $attributes['locale'] ?? \App::getLocale();
91
        $attributes = Arr::except($attributes, ['locale']);
92
        $non_translatable = [];
93
94
        $model = new static();
95
96
        // do the actual saving
97
        foreach ($attributes as $attribute => $value) {
98
            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
99
                $model->setTranslation($attribute, $locale, $value);
100
            } else { // the attribute is NOT translatable
101
                $non_translatable[$attribute] = $value;
102
            }
103
        }
104
        $model->fill($non_translatable)->save();
0 ignored issues
show
Bug introduced by
The method fill() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. 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

104
        $model->/** @scrutinizer ignore-call */ 
105
                fill($non_translatable)->save();
Loading history...
105
106
        return $model;
107
    }
108
109
    /**
110
     * Update translated items as json.
111
     *
112
     * @param  array  $attributes
113
     * @param  array  $options
114
     * @return bool
115
     */
116
    public function update(array $attributes = [], array $options = [])
117
    {
118
        if (! $this->exists) {
119
            return false;
120
        }
121
122
        $locale = $attributes['_locale'] ?? \App::getLocale();
123
        $attributes = Arr::except($attributes, ['_locale']);
124
        $non_translatable = [];
125
126
        // do the actual saving
127
        foreach ($attributes as $attribute => $value) {
128
            if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable
129
                $this->setTranslation($attribute, $locale, $value);
130
            } else { // the attribute is NOT translatable
131
                $non_translatable[$attribute] = $value;
132
            }
133
        }
134
135
        return $this->fill($non_translatable)->save($options);
136
    }
137
138
    /*
139
    |--------------------------------------------------------------------------
140
    |                            CUSTOM METHODS
141
    |--------------------------------------------------------------------------
142
    */
143
144
    /**
145
     * Check if a model is translatable, by the adapter's standards.
146
     *
147
     * @return bool
148
     */
149
    public function translationEnabledForModel()
150
    {
151
        return property_exists($this, 'translatable');
152
    }
153
154
    /**
155
     * Get all locales the admin is allowed to use.
156
     *
157
     * @return array
158
     */
159
    public function getAvailableLocales()
160
    {
161
        return config('backpack.crud.locales');
162
    }
163
164
    /**
165
     * Set the locale property. Used in normalizeLocale() to force the translation
166
     * to a different language that the one set in app()->getLocale();.
167
     *
168
     * @param string
169
     */
170
    public function setLocale($locale)
171
    {
172
        $this->locale = $locale;
173
    }
174
175
    /**
176
     * Get the locale property. Used in SpatieTranslatableSluggableService
177
     * to save the slug for the appropriate language.
178
     *
179
     * @param string
180
     */
181
    public function getLocale()
182
    {
183
        if ($this->locale) {
184
            return $this->locale;
185
        }
186
187
        return \Request::input('_locale', \App::getLocale());
188
    }
189
190
    /**
191
     * Magic method to get the db entries already translated in the wanted locale.
192
     *
193
     * @param  string  $method
194
     * @param  array  $parameters
195
     * @return
196
     */
197
    public function __call($method, $parameters)
198
    {
199
        switch ($method) {
200
            // translate all find methods
201
            case 'find':
202
            case 'findOrFail':
203
            case 'findMany':
204
            case 'findBySlug':
205
            case 'findBySlugOrFail':
206
                $translation_locale = \Request::input('_locale', \App::getLocale());
207
208
                if ($translation_locale) {
209
                    $item = parent::__call($method, $parameters);
210
211
                    if ($item instanceof \Traversable) {
212
                        foreach ($item as $instance) {
213
                            $instance->setLocale($translation_locale);
214
                        }
215
                    } elseif ($item) {
216
                        $item->setLocale($translation_locale);
217
                    }
218
219
                    return $item;
220
                }
221
222
                return parent::__call($method, $parameters);
223
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
224
225
                // do not translate any other methods
226
            default:
227
                return parent::__call($method, $parameters);
228
                break;
229
        }
230
    }
231
}
232