Completed
Push — master ( 4578cf...939e97 )
by João
01:21
created

Strings   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translations() 0 4 1
B get() 0 28 6
A addString() 0 14 1
1
<?php
2
3
namespace Presspack\Framework\Support\Translation;
4
5
use Illuminate\Support\Facades\App;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Strings extends Model
9
{
10
    protected $table = 'icl_strings';
11
    protected $guarded = [];
12
    public $timestamps = false;
13
14
    public function translations()
15
    {
16
        return $this->hasMany(StringTranslation::class, 'string_id');
17
    }
18
19
    public function get(string $string, string $locale = null)
20
    {
21
        $locale = $locale ?: App::getLocale();
22
23
        if ($locale == config('presspack.default_locale')) {
24
            return $string;
25
        }
26
27
        $table = $this->where('name', md5($string))
28
            ->where('context', 'presspack')
29
            ->first();
30
31
        if (! $table) {
32
            $this->addString($string);
33
34
            return $string;
35
        }
36
37
        $translation = $table->translations
38
            ->where('language', $locale)
39
            ->first();
40
41
        if (isset($translation->value) && 10 == $translation->status) {
42
            return $translation->value;
43
        }
44
45
        return $string;
46
    }
47
48
    public function addString($string)
49
    {
50
        $this->create([
51
            'language' => config('presspack.default_locale'),
52
            'context' => 'presspack',
53
            'name' => md5($string),
54
            'value' => $string,
55
            'type' => 'LINE',
56
            'status' => 0,
57
            'gettext_context' => '',
58
            'domain_name_context_md5' => md5('presspack'.md5($string)),
59
            'translation_priority' => '',
60
        ]);
61
    }
62
}
63