Completed
Push — master ( 35ba31...f8a85f )
by Ben
05:06
created

SquantoTranslator::get()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.2269

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 5
cts 6
cp 0.8333
rs 8.2222
cc 7
eloc 10
nc 14
nop 4
crap 7.2269
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
9
class SquantoTranslator extends LaravelTranslator implements Translator
10
{
11
    private $databaseTranslator;
12
13 69
    private $keyAsDefault = true;
14
    private $isDatabaseAlreadyMigrated = null;
15 69
16 69
    public function setKeyAsDefault($keyAsDefault = true)
17
    {
18
        $this->keyAsDefault = $keyAsDefault;
19
    }
20
21
    /**
22
     * Get the translation for the given key by following this priority chain:
23
     *
24
     * 1. Get from our cached translations
25
     * 2. Get from database
26
     * 3. Get from the /resources/lang
27
     *
28
     * @param  string $key
29
     * @param  array $replace
30
     * @param  string $locale
31 21
     * @param bool $fallback
32
     * @return string
33 21
     */
34
    public function get($key, array $replace = array(), $locale = null, $fallback = true)
35 21
    {
36 12
        $locale = $locale ?: $this->getLocale();
37
38
        if($result = $this->getFromExcludedSource($key, $replace, $locale, $fallback)) {
39 12
            return $result;
40
        }
41
42
        if ($result = $this->getFromCache($key, $replace, $locale, $fallback)) {
43 12
            return $result;
44
        }
45 12
46
        if ($result = $this->getFromDatabase($key, $replace, $locale, $fallback)) {
47
            return $result;
48
        }
49
50
        $result = parent::get($key, $replace, $locale, $fallback);
51
52
        return ($this->keyAsDefault || $result !== $key) ? $result : null;
53
    }
54
55
    /**
56 21
     * Get from excluded sources. This is used here to make retrieval of these
57
     * non-managed translations a lot faster by going straight to source
58 21
     *
59 21
     * @param $key
60
     * @param $replace
61
     * @param $locale
62 21
     * @param $fallback
63
     * @return array|null|string
64 21
     */
65
    private function getFromExcludedSource($key, $replace, $locale, $fallback)
66
    {
67 12
        if(!LineKey::fromString($key)->isExcludedSource()) return null;
68
69 12
        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...
70 12
    }
71
72
    /**
73 12
     * Retrieve the translation from the squanto cache.
74
     *
75
     * @param $key
76
     * @param array $replace
77
     * @param null $locale
78
     * @return mixed|null
79
     */
80
    private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true)
81
    {
82
        if (false === strpos($key, 'squanto::')) {
83
            $key = 'squanto::'.$key;
84
        }
85
86
        $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...
87
88
        return ($result !== $key) ? $result : null;
89
    }
90
91
    private function getFromDatabase($key, array $replace = array(), $locale = null, $fallback = true)
92
    {
93
        /**
94
         * If database tables are not present we will soft ignore this call and delegate to the native
95
         */
96
        if (! $this->isDatabaseAlreadyMigrated()) {
97
            return null;
98
        }
99
100
        if (!isset($this->databaseTranslator)) {
101
            $this->databaseTranslator = app(DatabaseTranslator::class);
102
        }
103
104
        return $this->databaseTranslator->get($key, $replace, $locale, $fallback);
105
    }
106
107
    /**
108
     * Verify that SQUANTO migrations are already run and present in this environment
109
     * Allow for a soft install
110
     *
111
     * @return null
112
     */
113
    private function isDatabaseAlreadyMigrated()
114
    {
115
        if (!is_null($this->isDatabaseAlreadyMigrated)) {
116
            return $this->isDatabaseAlreadyMigrated;
117
        }
118
119
        return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines'));
120
    }
121
}
122