Completed
Push — master ( 79fd8a...81e819 )
by Christian
05:05
created

Translation::getLocales()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
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\Jobs\AllTranslation;
7
use Omatech\Mage\Core\Domains\Translations\Jobs\ExportTranslation;
8
use Omatech\Mage\Core\Domains\Translations\Contracts\TranslationInterface;
9
use Omatech\Mage\Core\Domains\Translations\Features\FindOrFailTranslation;
10
use Omatech\Mage\Core\Domains\Translations\Contracts\AllTranslationInterface;
11
use Omatech\Mage\Core\Domains\Translations\Features\UpdateOrCreateTranslation;
12
use Omatech\Mage\Core\Domains\Translations\Contracts\ExportTranslationInterface;
13
use Omatech\Mage\Core\Domains\Translations\Features\ExistsAndDeleteTranslation;
14
15
class Translation implements TranslationInterface
16
{
17
    use FromArray;
18
19
    private $id;
20
    private $group;
21
    private $key;
22
    private $translations = [];
23
    private $syncAt;
24
    private $createdAt;
25
    private $updatedAt;
26
27
    /**
28
     * @return int|null
29
     */
30 9
    public function getId(): ?int
31
    {
32 9
        return $this->id;
33
    }
34
35
    /**
36
     * @param int $id
37
     * @return Translation
38
     */
39 9
    public function setId(int $id): self
40
    {
41 9
        $this->id = $id;
42
43 9
        return $this;
44
    }
45
46
    /**
47
     * @return string
48
     */
49 9
    public function getGroup(): string
50
    {
51 9
        return $this->group;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 9
    public function getKey(): string
58
    {
59 9
        return $this->key;
60
    }
61
62
    /**
63
     * @param string $key
64
     * @return Translation
65
     */
66 9
    public function setKey(string $key): self
67
    {
68 9
        $key = explode('.', $key);
69
70 9
        if (count($key) > 1) {
71 8
            $this->group = $key[0];
72 8
            $this->key = implode('.', array_slice($key, 1));
73
        } else {
74 2
            $this->group = 'single';
75 2
            $this->key = $key[0];
76
        }
77
78 9
        return $this;
79
    }
80
81
    /**
82
     * @param string $language
83
     * @param string $text
84
     * @return $this
85
     */
86 9
    public function setTranslation(string $language, string $text): self
87
    {
88 9
        $this->translations[$language] = $text;
89
90 9
        return $this;
91
    }
92
93
    /**
94
     * @return array
95
     */
96 9
    public function getTranslations(): array
97
    {
98 9
        return $this->translations;
99
    }
100
101
    /**
102
     * @param array $translations
103
     * @return $this
104
     */
105 9
    public function setTranslations(array $translations): self
106
    {
107 9
        foreach ($translations as $language => $text) {
108 2
            $this->setTranslation($language, $text);
109
        }
110
111 9
        return $this;
112
    }
113
114
    /**
115
     * @return array
116
     */
117 10
    private static function getLocales(): array
118
    {
119 10
        $availableLocales = config('mage.translations.available_locales');
120 10
        $locales = [];
121
122 10
        foreach ($availableLocales as $locale) {
123 10
            if (!array_key_exists($locale['locale'], $locales)) {
124 10
                $locales[] = $locale['locale'];
125
            }
126
        }
127
128 10
        return $locales;
129
    }
130
131
    /**
132
     *
133
     */
134 9
    private function setMissingTranslations(): void
135
    {
136 9
        foreach (static::getLocales() as $locale) {
137 9
            if (!array_key_exists($locale, $this->getTranslations())) {
138 9
                $this->setTranslation($locale, $this->getGroup() . '.' . $this->getKey());
139
            }
140
        }
141 9
    }
142
143
    /**
144
     * @return string
145
     */
146 9
    public function getSyncAt(): ?string
147
    {
148 9
        return $this->syncAt;
149
    }
150
151
    /**
152
     * @param string $syncAt
153
     * @return Translation
154
     */
155 9
    public function setSyncAt(?string $syncAt): self
156
    {
157 9
        $this->syncAt = $syncAt;
158
159 9
        return $this;
160
    }
161
162
    /**
163
     * @return string
164
     */
165 4
    public function getCreatedAt(): string
166
    {
167 4
        return $this->createdAt;
168
    }
169
170
    /**
171
     * @param string $createdAt
172
     * @return Translation
173
     */
174 9
    public function setCreatedAt(string $createdAt): self
175
    {
176 9
        $this->createdAt = $createdAt;
177
178 9
        return $this;
179
    }
180
181
    /**
182
     * @return string
183
     */
184 4
    public function getUpdatedAt(): string
185
    {
186 4
        return $this->updatedAt;
187
    }
188
189
    /**
190
     * @param string $updatedAt
191
     * @return Translation
192
     */
193 9
    public function setUpdatedAt(string $updatedAt): self
194
    {
195 9
        $this->updatedAt = $updatedAt;
196
197 9
        return $this;
198
    }
199
200
    /**
201
     * @param AllTranslationInterface $all
202
     * @return mixed
203
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
204
     */
205 1
    public static function all(AllTranslationInterface $all)
206
    {
207 1
        return app()->make(AllTranslation::class)
208 1
            ->make($all, static::getLocales());
209
    }
210
211
    /**
212
     * @param int $id
213
     * @return Translation
214
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
215
     */
216 2
    public static function find(int $id): self
217
    {
218 2
        return app()->make(FindOrFailTranslation::class)->make($id);
219
    }
220
221
    /**
222
     * @return bool
223
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
224
     */
225 9
    public function save(): bool
226
    {
227 9
        $this->setMissingTranslations();
228
229 9
        return app()->make(UpdateOrCreateTranslation::class)->make($this);
230
    }
231
232
    /**
233
     * @return bool
234
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
235
     */
236 3
    public function delete(): bool
237
    {
238 3
        return app()->make(ExistsAndDeleteTranslation::class)->make($this);
239
    }
240
241
    /**
242
     * @param AllTranslationInterface $allTranslationInterface
243
     * @param ExportTranslationInterface $exportTranslationInterface
244
     * @param array|null $locales
245
     * @return mixed
246
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
247
     */
248
    public static function export(
249
        AllTranslationInterface $allTranslationInterface,
250
        ExportTranslationInterface $exportTranslationInterface,
251
        array $locales = null
252
    ) {
253
        $locales = $locales ?? static::getLocales();
254
255
        return app()->make(ExportTranslation::class)
256
            ->make($allTranslationInterface, $exportTranslationInterface, $locales);
257
    }
258
}
259