Translatable   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 120
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fillTranslations() 0 8 3
B createOrUpdateTranslation() 0 22 4
A translation() 0 10 4
A translatableGetter() 0 10 2
A getRawField() 0 4 1
A getTranslatedFieldNamesForEntity() 0 11 2
A getForeignKey() 0 7 1
A getTranslationClass() 0 4 1
1
<?php namespace Modules\Core\Doctrine;
2
3
use Illuminate\Support\Facades\App;
4
use Mitch\LaravelDoctrine\EntityManagerFacade;
5
use ReflectionClass;
6
7
trait Translatable
8
{
9
    /**
10
     * @param array $input
11
     */
12
    public function fillTranslations(array $input)
13
    {
14
        foreach ($input as $locale => $attributes) {
15
            foreach ($attributes as $key => $value) {
16
                $this->createOrUpdateTranslation($key, $locale, $value);
17
            }
18
        }
19
    }
20
21
    /**
22
     * Create or update the given field name
23
     * @param string $fieldName
24
     * @param string $locale
25
     * @param string $value
26
     */
27
    public function createOrUpdateTranslation($fieldName, $locale, $value)
28
    {
29
        $found = false;
30
        foreach ($this->translation as $translation) {
0 ignored issues
show
Bug introduced by
The property translation 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...
31
            if ($translation->locale == $locale) {
32
                $translation->$fieldName = $value;
33
                $found = true;
34
                continue;
35
            }
36
        }
37
38
        if (! $found) {
39
            $foreignKey = $this->getForeignKey();
40
41
            $translationObjectClass = $this->getTranslationClass();
42
            $translationObject = new $translationObjectClass();
43
            $translationObject->locale = $locale;
44
            $translationObject->$fieldName = $value;
45
            $translationObject->$foreignKey = $this;
46
            $this->translation->add($translationObject);
47
        }
48
    }
49
50
    /**
51
     * Get the translation of the given field name
52
     * @param  string      $fieldName
53
     * @param  string|null $locale
54
     * @return string
55
     */
56
    public function translation($fieldName, $locale = null)
57
    {
58
        $locale = $locale ?: App::getLocale();
59
60
        foreach ($this->translation as $translation) {
61
            if ($translation->locale == $locale) {
62
                return $translation->$fieldName;
63
            }
64
        }
65
    }
66
67
    /**
68
     * @param  string $fieldName
69
     * @return mixed
70
     */
71
    public function translatableGetter($fieldName)
72
    {
73
        if (in_array($fieldName, $this->getTranslatedFieldNamesForEntity())) {
74
            $result = $this->translation($fieldName);
75
        } else {
76
            $result = $this->getRawField($fieldName);
77
        }
78
79
        return $result;
80
    }
81
82
    /**
83
     * @param $name
84
     * @return mixed
85
     */
86
    public function getRawField($name)
87
    {
88
        return $this->$name;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    private function getTranslatedFieldNamesForEntity()
95
    {
96
        $cacheArray = [];
97
        $translatedEntityName = $this->getTranslationClass();
98
        if (! isset($cacheArray[$translatedEntityName])) {
99
            $cacheArray[$translatedEntityName] = array_values(array_diff(EntityManagerFacade::getClassMetadata($translatedEntityName)->getColumnNames(),
100
                ['id', 'locale']));
101
        }
102
103
        return $cacheArray[$translatedEntityName];
104
    }
105
106
    /**
107
     * Get the foreign key for the current class
108
     * @return string
109
     */
110
    private function getForeignKey()
111
    {
112
        $reflectionClass = new ReflectionClass(get_class($this));
113
        $foreignKey = strtolower($reflectionClass->getShortName());
114
115
        return $foreignKey;
116
    }
117
118
    /**
119
     * Get the Translations class name
120
     * @return string
121
     */
122
    private function getTranslationClass()
123
    {
124
        return get_class($this) . 'Translation';
125
    }
126
}
127