Completed
Push — master ( 808eaf...89b4c3 )
by Christian
07:18
created

Translation::setMissingTranslations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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