Completed
Push — master ( fa485b...23e6da )
by Ben
11:58
created

SquantoTranslator::get()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.2944

Importance

Changes 0
Metric Value
cc 7
eloc 11
c 0
b 0
f 0
nc 14
nop 4
dl 0
loc 23
ccs 9
cts 11
cp 0.8182
crap 7.2944
rs 6.7272
1
<?php
2
3
namespace Thinktomorrow\Squanto\Translators;
4
5
use Illuminate\Support\Facades\Schema;
6
use Illuminate\Translation\Translator as LaravelTranslator;
7
use Thinktomorrow\Squanto\Domain\LineKey;
8
use Thinktomorrow\Squanto\Domain\PageKey;
9
10
class SquantoTranslator extends LaravelTranslator implements Translator
11
{
12
    private $databaseTranslator;
13
14
    private $keyAsDefault = true;
15
    private $isDatabaseAlreadyMigrated = null;
16
17 54
    public function setKeyAsDefault($keyAsDefault = true)
18
    {
19 54
        $this->keyAsDefault = $keyAsDefault;
20 54
    }
21
22
    /**
23
     * Get the translation for the given key by following this priority chain:
24
     *
25
     * 1. Get from our cached translations
26
     * 2. Get from database
27
     * 3. Get from the /resources/lang
28
     *
29
     * @param  string $key
30
     * @param  array $replace
31
     * @param  string $locale
32
     * @param bool $fallback
33
     * @return string
34
     */
35 10
    public function get($key, array $replace = array(), $locale = null, $fallback = true)
36
    {
37 10
        $locale = $locale ?: $this->getLocale();
38
39
        // The key is always stored as lowercase so make sure our key input is sanitized as well.
40 10
        $key = strtolower($key);
41
42 10
        if($result = $this->getFromExcludedSource($key, $replace, $locale, $fallback)) {
43
            return $result;
44
        }
45
46 10
        if ($result = $this->getFromCache($key, $replace, $locale, $fallback)) {
47 7
            return $result;
48
        }
49
50 4
        if ($result = $this->getFromDatabase($key, $replace, $locale, $fallback)) {
51
            return $result;
52
        }
53
54 4
        $result = parent::get($key, $replace, $locale, $fallback);
55
56 4
        return ($this->keyAsDefault || $result !== $key) ? $result : null;
57
    }
58
59
    /**
60
     * Get from excluded sources. This is used here to make retrieval of these
61
     * non-managed translations a lot faster by going straight to source
62
     *
63
     * @param $key
64
     * @param $replace
65
     * @param $locale
66
     * @param $fallback
67
     * @return array|null|string
68
     */
69 10
    private function getFromExcludedSource($key, $replace, $locale, $fallback)
70
    {
71 10
        if(!PageKey::fromLineKeyString($key)->isExcludedSource()) return null;
72
73
        return parent::get($key, $replace, $locale, $fallback);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (get() instead of getFromExcludedSource()). Are you sure this is correct? If so, you might want to change this to $this->get().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
74
    }
75
76
    /**
77
     * Retrieve the translation from the squanto cache.
78
     *
79
     * @param $key
80
     * @param array $replace
81
     * @param null $locale
82
     * @return mixed|null
83
     */
84 10
    private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true)
85
    {
86 10
        if (false === strpos($key, 'squanto::')) {
87 10
            $key = 'squanto::'.$key;
88
        }
89
90 10
        $result = parent::get($key, $replace, $locale, $fallback);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (get() instead of getFromCache()). Are you sure this is correct? If so, you might want to change this to $this->get().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
91
92 10
        return ($result !== $key) ? $result : null;
93
    }
94
95 4
    private function getFromDatabase($key, array $replace = array(), $locale = null, $fallback = true)
96
    {
97
        /**
98
         * If database tables are not present we will soft ignore this call and delegate to the native
99
         */
100 4
        if (! $this->isDatabaseAlreadyMigrated()) {
101
            return null;
102
        }
103
104 4
        if (!isset($this->databaseTranslator)) {
105 4
            $this->databaseTranslator = app(DatabaseTranslator::class);
106
        }
107
108 4
        return $this->databaseTranslator->get($key, $replace, $locale, $fallback);
109
    }
110
111
    /**
112
     * Verify that SQUANTO migrations are already run and present in this environment
113
     * Allow for a soft install
114
     *
115
     * @return null
116
     */
117 4
    private function isDatabaseAlreadyMigrated()
118
    {
119 4
        if (!is_null($this->isDatabaseAlreadyMigrated)) {
120
            return $this->isDatabaseAlreadyMigrated;
121
        }
122
123 4
        return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines'));
124
    }
125
}
126