EloquentTranslationRepository::updateFromImport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 9.4285
1
<?php namespace Modules\Translation\Repositories\Eloquent;
2
3
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
4
use Modules\Translation\Entities\TranslationTranslation;
5
use Modules\Translation\Repositories\TranslationRepository;
6
7
class EloquentTranslationRepository extends EloquentBaseRepository implements TranslationRepository
8
{
9
    /**
10
     * @param string $key
11
     * @param string $locale
12
     * @return string
13
     */
14
    public function findByKeyAndLocale($key, $locale = null)
15
    {
16
        $locale = $locale ?: app()->getLocale();
17
18
        $translation = $this->model->whereKey($key)->with('translations')->first();
19
        if ($translation && $translation->hasTranslation($locale)) {
20
            return $translation->translate($locale)->value;
21
        }
22
23
        return '';
24
    }
25
26
    public function allFormatted()
27
    {
28
        $allRows = $this->all();
29
        $allDatabaseTranslations = [];
30
        foreach ($allRows as $translation) {
31
            foreach (config('laravellocalization.supportedLocales') as $locale => $language) {
32
                if ($translation->hasTranslation($locale)) {
33
                    $allDatabaseTranslations[$locale][$translation->key] = $translation->translate($locale)->value;
34
                }
35
            }
36
        }
37
38
        return $allDatabaseTranslations;
39
    }
40
41
    public function saveTranslationForLocaleAndKey($locale, $key, $value)
42
    {
43
        $translation = $this->findTranslationByKey($key);
44
        $translation->translateOrNew($locale)->value = $value;
45
        $translation->save();
46
    }
47
48
    public function findTranslationByKey($key)
49
    {
50
        return $this->model->firstOrCreate(['key' => $key]);
0 ignored issues
show
Bug introduced by
The method firstOrCreate() does not exist on Illuminate\Database\Eloquent\Model. Did you maybe mean create()?

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...
51
    }
52
53
    /**
54
     * Update the given translation key with the given data
55
     * @param string $key
56
     * @param array $data
57
     * @return mixed
58
     */
59
    public function updateFromImport($key, array $data)
60
    {
61
        $translation = $this->findTranslationByKey($key);
62
        $translation->update($data);
63
    }
64
65
    /**
66
     * Set the given value on the given TranslationTranslation
67
     * @param TranslationTranslation $translationTranslation
68
     * @param string $value
69
     * @return void
70
     */
71
    public function updateTranslationToValue(TranslationTranslation $translationTranslation, $value)
72
    {
73
        $translationTranslation->value = $value;
74
        $translationTranslation->save();
75
    }
76
}
77