EloquentTranslationRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 12
c 8
b 0
f 0
lcom 1
cbo 2
dl 0
loc 70
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findByKeyAndLocale() 0 11 4
A allFormatted() 0 14 4
A findTranslationByKey() 0 4 1
A saveTranslationForLocaleAndKey() 0 6 1
A updateFromImport() 0 5 1
A updateTranslationToValue() 0 5 1
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