Translation   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 260
ccs 69
cts 69
cp 1
rs 10
c 0
b 0
f 0
wmc 29
lcom 1
cbo 7

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A setId() 0 6 1
A getGroup() 0 4 1
A getKey() 0 4 1
A setKey() 0 14 2
A setTranslation() 0 6 1
A getTranslations() 0 4 1
A setTranslations() 0 8 2
A getLocales() 0 13 3
A setMissingTranslations() 0 8 3
A getSyncAt() 0 4 1
A setSyncAt() 0 6 1
A getCreatedAt() 0 4 1
A setCreatedAt() 0 6 1
A getUpdatedAt() 0 4 1
A setUpdatedAt() 0 6 1
A all() 0 4 1
A find() 0 7 2
A save() 0 6 1
A delete() 0 4 1
A export() 0 9 1
A import() 0 8 1
1
<?php
2
3
namespace Omatech\Mage\Core\Domains\Translations;
4
5
use Omatech\Mage\Core\Domains\Shared\Traits\FromArray;
6
use Omatech\Mage\Core\Domains\Translations\Contracts\AllTranslationInterface;
7
use Omatech\Mage\Core\Domains\Translations\Contracts\ExportTranslationInterface;
8
use Omatech\Mage\Core\Domains\Translations\Contracts\FindTranslationInterface;
9
use Omatech\Mage\Core\Domains\Translations\Contracts\ImportTranslationInterface;
10
use Omatech\Mage\Core\Domains\Translations\Contracts\TranslationInterface;
11
use Omatech\Mage\Core\Domains\Translations\Features\ExistsAndDeleteTranslation;
12
use Omatech\Mage\Core\Domains\Translations\Features\FindOrFailTranslation;
13
use Omatech\Mage\Core\Domains\Translations\Features\UpdateOrCreateTranslation;
14
use Omatech\Mage\Core\Domains\Translations\Jobs\ExportTranslation;
15
use Omatech\Mage\Core\Domains\Translations\Jobs\ImportTranslation;
16
17
class Translation implements TranslationInterface
18
{
19
    use FromArray;
20
21
    private $id;
22
    private $group;
23
    private $key;
24
    private $translations = [];
25
    private $syncAt;
26
    private $createdAt;
27
    private $updatedAt;
28
29
    /**
30
     * @return int|null
31
     */
32 19
    public function getId(): ?int
33
    {
34 19
        return $this->id;
35
    }
36
37
    /**
38
     * @param int $id
39
     * @return $this
40
     */
41 19
    public function setId(int $id)
42
    {
43 19
        $this->id = $id;
44
45 19
        return $this;
46
    }
47
48
    /**
49
     * @return string
50
     */
51 19
    public function getGroup(): string
52
    {
53 19
        return $this->group;
54
    }
55
56
    /**
57
     * @return string
58
     */
59 19
    public function getKey(): string
60
    {
61 19
        return $this->key;
62
    }
63
64
    /**
65
     * @param string $key
66
     * @return $this
67
     */
68 19
    public function setKey(string $key)
69
    {
70 19
        $key = explode('.', $key);
71
72 19
        if (count($key) > 1) {
73 18
            $this->group = $key[0];
74 18
            $this->key = implode('.', array_slice($key, 1));
75
        } else {
76 2
            $this->group = 'single';
77 2
            $this->key = $key[0];
78
        }
79
80 19
        return $this;
81
    }
82
83
    /**
84
     * @param string $language
85
     * @param string $text
86
     * @return $this
87
     */
88 19
    public function setTranslation(string $language, string $text)
89
    {
90 19
        $this->translations[$language] = $text;
91
92 19
        return $this;
93
    }
94
95
    /**
96
     * @return array
97
     */
98 19
    public function getTranslations(): array
99
    {
100 19
        return $this->translations;
101
    }
102
103
    /**
104
     * @param array $translations
105
     * @return $this
106
     */
107 18
    public function setTranslations(array $translations)
108
    {
109 18
        foreach ($translations as $language => $text) {
110 9
            $this->setTranslation($language, $text);
111
        }
112
113 18
        return $this;
114
    }
115
116
    /**
117
     * @return array
118
     */
119 20
    private static function getLocales(): array
120
    {
121 20
        $availableLocales = config('mage.translations.available_locales');
122 20
        $locales = [];
123
124 20
        foreach ($availableLocales as $locale) {
125 20
            if (! array_key_exists($locale['locale'], $locales)) {
126 20
                $locales[] = $locale['locale'];
127
            }
128
        }
129
130 20
        return $locales;
131
    }
132
133
    /**
134
     * @return void
135
     */
136 19
    private function setMissingTranslations(): void
137
    {
138 19
        foreach (static::getLocales() as $locale) {
0 ignored issues
show
Bug introduced by
Since getLocales() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLocales() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
139 19
            if (! array_key_exists($locale, $this->getTranslations())) {
140 17
                $this->setTranslation($locale, $this->getGroup().'.'.$this->getKey());
141
            }
142
        }
143 19
    }
144
145
    /**
146
     * @return string
147
     */
148 19
    public function getSyncAt(): ?string
149
    {
150 19
        return $this->syncAt;
151
    }
152
153
    /**
154
     * @param string|null $syncAt
155
     * @return $this
156
     */
157 19
    public function setSyncAt(?string $syncAt)
158
    {
159 19
        $this->syncAt = $syncAt;
160
161 19
        return $this;
162
    }
163
164
    /**
165
     * @return string
166
     */
167 4
    public function getCreatedAt(): string
168
    {
169 4
        return $this->createdAt;
170
    }
171
172
    /**
173
     * @param string $createdAt
174
     * @return $this
175
     */
176 19
    public function setCreatedAt(string $createdAt)
177
    {
178 19
        $this->createdAt = $createdAt;
179
180 19
        return $this;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 4
    public function getUpdatedAt(): string
187
    {
188 4
        return $this->updatedAt;
189
    }
190
191
    /**
192
     * @param string $updatedAt
193
     * @return $this
194
     */
195 19
    public function setUpdatedAt(string $updatedAt)
196
    {
197 19
        $this->updatedAt = $updatedAt;
198
199 19
        return $this;
200
    }
201
202
    /**
203
     * @param AllTranslationInterface $all
204
     * @return mixed
205
     */
206 1
    public static function all(AllTranslationInterface $all)
207
    {
208 1
        return $all->get(static::getLocales());
0 ignored issues
show
Bug introduced by
Since getLocales() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLocales() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
209
    }
210
211
    /**
212
     * @param FindTranslationInterface $find
213
     * @param array $params
214
     * @return $this
215
     */
216 9
    public static function find(FindTranslationInterface $find, array $params): self
217
    {
218 9
        if (isset($params['key'])) {
219 9
            $params['key'] = strtolower(str_replace(' ', '.', $params['key']));
220
        }
221 9
        return (new FindOrFailTranslation())->make($find, $params);
222
    }
223
224
    /**
225
     * @return bool
226
     * @throws Exceptions\TranslationAlreadyExistsException
227
     * @throws Exceptions\TranslationDoesNotExistsException
228
     * @throws Exceptions\TranslationExistsMustBeUniqueException
229
     */
230 19
    public function save(): bool
231
    {
232 19
        $this->setMissingTranslations();
233
234 19
        return (new UpdateOrCreateTranslation())->make($this);
235
    }
236
237
    /**
238
     * @throws Exceptions\TranslationDoesNotExistsException
239
     */
240 3
    public function delete(): bool
241
    {
242 3
        return (new ExistsAndDeleteTranslation())->make($this);
243
    }
244
245
    /**
246
     * @param AllTranslationInterface $all
247
     * @param ExportTranslationInterface $export
248
     * @param array|null $locales
249
     * @return string
250
     */
251 2
    public static function export(
252
        AllTranslationInterface $all,
253
        ExportTranslationInterface $export,
254
        array $locales = null
255
    ) {
256 2
        $locales = $locales ?? static::getLocales();
0 ignored issues
show
Bug introduced by
Since getLocales() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLocales() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
257
258 2
        return (new ExportTranslation())->make($all, $export, $locales);
259
    }
260
261
    /**
262
     * @param FindTranslationInterface $find
263
     * @param ImportTranslationInterface $import
264
     * @param string $path
265
     * @param string $locale
266
     * @return bool
267
     */
268 7
    public static function import(
269
        FindTranslationInterface $find,
270
        ImportTranslationInterface $import,
271
        string $path,
272
        string $locale = ''
273
    ) {
274 7
        return (new ImportTranslation())->make($find, $import, $path, $locale);
275
    }
276
}
277