Completed
Push — master ( 5e919a...7cc452 )
by Nicolas
03:29
created

EloquentTranslationRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 12
c 9
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 saveTranslationForLocaleAndKey() 0 6 1
A findTranslationByKey() 0 4 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]);
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;
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<Modules\Translati...TranslationTranslation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
        $translationTranslation->save();
75
    }
76
}
77