Completed
Push — master ( 3c969b...fa485b )
by Philippe
15:39
created

Translatable   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 148
ccs 15
cts 40
cp 0.375
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultTranslation() 0 8 2
A updateTranslation() 0 8 2
A saveTranslation() 0 6 1
A getTranslationFor() 0 4 2
A setTranslation() 0 6 1
A removeTranslation() 0 8 2
A getUsedLocales() 0 4 1
A getNonUsedLocales() 0 4 1
A getAvailableLocales() 0 4 1
A fetchLocales() 0 9 2
A validateLocale() 0 6 2
A toRawArray() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Trait Translatable
9
 * @author Ben Cavens
10
 *
11
 * Allows the entity to contain multiple translations
12
 * requires the parent entity to include the Dimsav/Translatable/Translatable trait
13
 *
14
 */
15
trait Translatable
16
{
17
    public function getDefaultTranslation($attribute)
18
    {
19
        if (!($translation = $this->getTranslation(config('app.fallback_locale')))) {
0 ignored issues
show
Bug introduced by
The method getTranslation() does not exist on Thinktomorrow\Squanto\Domain\Translatable. Did you maybe mean getTranslationFor()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
20
            return null;
21
        }
22
23
        return $translation->$attribute;
24
    }
25
26
    /**
27
     * Save multiple attributes at once
28
     *
29
     * @param $locale
30
     * @param array $values
31
     */
32
    public function updateTranslation($locale, array $values)
33
    {
34
        foreach ($values as $attribute => $value) {
35
            $this->setTranslation($locale, $attribute, $value);
36
        }
37
38
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
    }
40
41
    /**
42
     * Save a single attribute
43
     *
44
     * @param $locale
45
     * @param $attribute
46
     * @param $value
47
     */
48 18
    public function saveTranslation($locale, $attribute, $value)
49
    {
50 18
        $this->setTranslation($locale, $attribute, $value);
51
52 18
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53 18
    }
54
55
    /**
56
     * @param $attribute
57
     * @param null $locale
58
     * @param bool $fallback | if locale not present, use fallback locale instead
59
     * @return string null
60
     */
61 7
    public function getTranslationFor($attribute, $locale = null, $fallback = true)
62
    {
63 7
        return (!$trans = $this->getTranslation($locale, $fallback)) ? null : $trans->$attribute;
0 ignored issues
show
Bug introduced by
The method getTranslation() does not exist on Thinktomorrow\Squanto\Domain\Translatable. Did you maybe mean getTranslationFor()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
64
    }
65
66
    /**
67
     * Create or update a translation attribute.
68
     * Note: only sets to entity, does not save it.
69
     *
70
     * @param $locale
71
     * @param $attribute
72
     * @param $value
73
     */
74 18
    private function setTranslation($locale, $attribute, $value)
75
    {
76 18
        $this->validateLocale($locale);
77
78 18
        $this->translateOrNew($locale)->$attribute = $value;
0 ignored issues
show
Bug introduced by
It seems like translateOrNew() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79 18
    }
80
81
    public function removeTranslation($locale)
82
    {
83
        if (!$this->hasTranslation($locale)) {
0 ignored issues
show
Bug introduced by
It seems like hasTranslation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
84
            return;
85
        }
86
87
        return $this->getTranslation($locale)->delete();
0 ignored issues
show
Bug introduced by
The method getTranslation() does not exist on Thinktomorrow\Squanto\Domain\Translatable. Did you maybe mean getTranslationFor()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
88
    }
89
90
    /**
91
     * Get all locales where this entity
92
     * already has any translations of
93
     *
94
     * @return array
95
     */
96
    public function getUsedLocales()
97
    {
98
        return $this->fetchLocales(true);
99
    }
100
101
    /**
102
     * Get all available locales where this entity
103
     * does not have any translations of
104
     *
105
     * @return array
106
     */
107
    public function getNonUsedLocales()
108
    {
109
        return $this->fetchLocales(false);
110
    }
111
112
    /**
113
     * Get the locales from squanto config.
114
     * Fallback is the list provided via the translation package
115
     *
116
     * @return array
117
     */
118 18
    public function getAvailableLocales()
119
    {
120 18
        return config('squanto.locales', config('translatable.locales', []));
121
    }
122
123
    /**
124
     * Get all locales associated with this entity
125
     *
126
     * @param bool $available
127
     * @return array
128
     */
129
    private function fetchLocales($available = true)
130
    {
131
        $available_locales = $this->getAvailableLocales();
132
        $current_locales = $this->translations()->lists('locale')->toArray();
0 ignored issues
show
Bug introduced by
It seems like translations() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
133
134
        return array_filter($available_locales, function ($v) use ($current_locales, $available) {
135
            return $available ? in_array($v, $current_locales) : !in_array($v, $current_locales);
136
        }, ARRAY_FILTER_USE_BOTH);
137
    }
138
139
    /**
140
     * Is passed locale one of the allowed ones from config?
141
     *
142
     * @param $locale
143
     */
144 18
    private function validateLocale($locale)
145
    {
146 18
        if (!in_array($locale, $this->getAvailableLocales())) {
147
            throw new InvalidArgumentException('Improper locale [' . $locale . '] given or locale is not available');
148
        }
149 18
    }
150
151
    /**
152
     * Dimsav translatable trait overrides the toArray in order to
153
     * inject default translations. To ignore this behaviour and
154
     * present the actual values you should use this method.
155
     *
156
     * @return array
157
     */
158
    public function toRawArray()
159
    {
160
        return parent::toArray();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (toArray() instead of toRawArray()). Are you sure this is correct? If so, you might want to change this to $this->toArray().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
161
    }
162
}
163