Strings   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 32
dl 0
loc 71
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTranslated() 0 11 4
A addString() 0 12 1
A checkIfExists() 0 11 2
A get() 0 9 2
A __construct() 0 5 2
1
<?php
2
3
namespace Presspack\Framework\Support\Translation;
4
5
use Illuminate\Support\Facades\App;
6
use Presspack\Framework\Support\Translation\Models\IclString;
7
8
class Strings
9
{
10
    /** @var \Illuminate\Support\Collection */
11
    protected $iclStrings;
12
13
    /** @var \Illuminate\Support\Collection */
14
    protected $string;
15
16
    /** @var string */
17
    protected $translated;
18
19
    /** @var bool */
20
    protected $localeIsDefault = true;
21
22
    public function __construct()
23
    {
24
        if (App::getLocale() != config('presspack.default_locale')) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        if (App::getLocale() != /** @scrutinizer ignore-call */ config('presspack.default_locale')) {
Loading history...
25
            $this->localeIsDefault = false;
26
            $this->iclStrings = IclString::where('context', 'presspack')->get();
27
        }
28
    }
29
30
    public function get(string $string): string
31
    {
32
        $this->translated = $string;
33
34
        if ($this->localeIsDefault) {
35
            return $this->translated;
36
        }
37
38
        return $this->getTranslated();
39
    }
40
41
    public function getTranslated(): string
42
    {
43
        if ($this->checkIfExists()) {
44
            $translation = $this->string->translations->where('language', App::getLocale())->first();
45
46
            if (isset($translation->value) && 10 == $translation->status) {
47
                $this->translated = $translation->value;
48
            }
49
        }
50
51
        return $this->translated;
52
    }
53
54
    public function checkIfExists(): bool
55
    {
56
        $this->string = $this->iclStrings->where('name', md5($this->translated))->first();
57
58
        if (! $this->string) {
59
            $this->addString($this->translated);
60
61
            return false;
62
        }
63
64
        return true;
65
    }
66
67
    public function addString($string)
68
    {
69
        IclString::create([
70
            'language' => config('presspack.default_locale'),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            'language' => /** @scrutinizer ignore-call */ config('presspack.default_locale'),
Loading history...
71
            'context' => 'presspack',
72
            'name' => md5($string),
73
            'value' => $string,
74
            'type' => 'LINE',
75
            'status' => 0,
76
            'gettext_context' => '',
77
            'domain_name_context_md5' => md5('presspack'.md5($string)),
78
            'translation_priority' => '',
79
        ]);
80
    }
81
}
82