GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#64)
by
unknown
01:15
created

LanguageLine   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 80
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 1
A setTranslation() 0 6 1
A flushGroupCache() 0 6 2
A getTranslatedLocales() 0 4 1
A getTranslationsForGroup() 0 13 1
A getCacheKey() 0 4 1
B getTranslation() 0 8 5
1
<?php
2
3
namespace Spatie\TranslationLoader;
4
5
use Illuminate\Support\Facades\Cache;
6
use Illuminate\Database\Eloquent\Model;
7
8
class LanguageLine extends Model
9
{
10
    /** @var array */
11
    public $translatable = ['text'];
12
13
    /** @var array */
14
    public $guarded = ['id'];
15
16
    /** @var array */
17
    protected $casts = ['text' => 'array'];
18
19
    public static function boot()
20
    {
21
        static::saved(function (LanguageLine $languageLine) {
22
            $languageLine->flushGroupCache();
23
        });
24
25
        static::deleted(function (LanguageLine $languageLine) {
26
            $languageLine->flushGroupCache();
27
        });
28
    }
29
30
    public static function getTranslationsForGroup(string $locale, string $group): array
31
    {
32
        return Cache::rememberForever(static::getCacheKey($group, $locale), function () use ($group, $locale) {
33
            return static::query()
34
                ->where('group', $group)
35
                ->get()
36
                ->reduce(function ($lines, LanguageLine $languageLine) use ($locale) {
37
                    array_set($lines, $languageLine->key, $languageLine->getTranslation($locale));
38
39
                    return $lines;
40
                }) ?? [];
41
        });
42
    }
43
44
    public static function getCacheKey(string $group, string $locale): string
45
    {
46
        return "spatie.translation-loader.{$group}.{$locale}";
47
    }
48
49
    /**
50
     * @param string $locale
51
     *
52
     * @return string
53
     */
54
    public function getTranslation(string $locale): string
55
    {
56
        if(isset($this->text[$locale]) && !is_array($this->text[$locale]))
57
            return $this->text[$locale];
58
        if(isset($this->text[config('app.fallback_locale')]) && !is_array($this->text[config('app.fallback_locale')]))
59
            return $this->text[config('app.fallback_locale')];
60
        return '';
61
    }
62
63
    /**
64
     * @param string $locale
65
     * @param string $value
66
     *
67
     * @return $this
68
     */
69
    public function setTranslation(string $locale, string $value)
70
    {
71
        $this->text = array_merge($this->text ?? [], [$locale => $value]);
72
73
        return $this;
74
    }
75
76
    protected function flushGroupCache()
77
    {
78
        foreach ($this->getTranslatedLocales() as $locale) {
79
            Cache::forget(static::getCacheKey($this->group, $locale));
80
        }
81
    }
82
83
    protected function getTranslatedLocales(): array
84
    {
85
        return array_keys($this->text);
86
    }
87
}
88