Completed
Push — master ( 43d6b6...7fd3dc )
by Sherif
12:33
created

Translatable::getTranslatedAttribute()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 9
nc 7
nop 2
1
<?php namespace App\Modules\V1\Core\Traits;
2
3
trait Translatable  
4
{
5
    /**
6
     * Create a new model instance that is existing.
7
     *
8
     * @param  array  $attributes
9
     * @param  string|null  $connection
10
     * @return static
11
     */
12
    public function newFromBuilder($attributes = [], $connection = null)
13
    {
14
        $model = parent::newFromBuilder($attributes, $connection);
15
16
        foreach ($model->attributes AS $key => $value)
17
        {
18
            if (isset($this->translatable) && in_array($key, $this->translatable)) 
0 ignored issues
show
Bug introduced by
The property translatable does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
            {
20
                $model->$key = $this->getTranslatedAttribute($key, $value);
21
            }
22
        }
23
24
        return $model;
25
    }
26
27
    /**
28
     * Returns a translatable model attribute based on the application's locale settings.
29
     *
30
     * @param $key
31
     * @param $values
32
     * @return string
33
     */
34
    protected function getTranslatedAttribute($key, $values)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
        $values         = json_decode($values);
37
        $primaryLocale  = \Session::get(\CoreConfig::getConfig()['var_names']['locale']);
38
        $fallbackLocale = \CoreConfig::getConfig()['var_names']['locale_fallback'];
39
40
        if ($primaryLocale == 'all') 
41
        {
42
            return $values;
43
        }
44
45
        if ( ! $primaryLocale || ! isset($values->$primaryLocale)) 
46
        {
47
            return $values ? isset($values->$fallbackLocale) ? $values->$fallbackLocale : $values : '';
48
        }
49
50
        return $primaryLocale == 'all' ? $values : $values->$primaryLocale;
51
    }
52
}