Completed
Push — master ( 2093be...99e81f )
by Ben
07:44
created

SquantoTranslator::get()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.392

Importance

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