Completed
Push — master ( 8c886d...91409e )
by Ben
03:10
created

SquantoTranslator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 42
cts 46
cp 0.913
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setKeyAsDefault() 0 4 1
B get() 0 20 7
A getFromExcludedSource() 0 6 2
A getFromCache() 0 10 3
A getFromDatabase() 0 15 3
A isDatabaseAlreadyMigrated() 0 8 2
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 46
14
    private $keyAsDefault = true;
15 46
    private $isDatabaseAlreadyMigrated = null;
16 46
17 47
    public function setKeyAsDefault($keyAsDefault = true)
18
    {
19 47
        $this->keyAsDefault = $keyAsDefault;
20 47
    }
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 14
     * @param  string $locale
32
     * @param bool $fallback
33 14
     * @return string
34
     */
35 23
    public function get($key, array $replace = array(), $locale = null, $fallback = true)
36 8
    {
37 9
        $locale = $locale ?: $this->getLocale();
38
39 17
        if($result = $this->getFromExcludedSource($key, $replace, $locale, $fallback)) {
40
            return $result;
41
        }
42
43 17
        if ($result = $this->getFromCache($key, $replace, $locale, $fallback)) {
44 6
            return $result;
45 8
        }
46
47 4
        if ($result = $this->getFromDatabase($key, $replace, $locale, $fallback)) {
48
            return $result;
49
        }
50
51 4
        $result = parent::get($key, $replace, $locale, $fallback);
52
53 4
        return ($this->keyAsDefault || $result !== $key) ? $result : null;
54
    }
55
56 14
    /**
57
     * Get from excluded sources. This is used here to make retrieval of these
58 14
     * non-managed translations a lot faster by going straight to source
59 14
     *
60
     * @param $key
61
     * @param $replace
62 14
     * @param $locale
63
     * @param $fallback
64 14
     * @return array|null|string
65
     */
66 9
    private function getFromExcludedSource($key, $replace, $locale, $fallback)
67 8
    {
68 9
        if(!PageKey::fromLineKeyString($key)->isExcludedSource()) return null;
69 8
70 8
        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 8
    /**
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 9
    private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true)
82
    {
83 9
        if (false === strpos($key, 'squanto::')) {
84 9
            $key = 'squanto::'.$key;
85
        }
86
87 9
        $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 9
        return ($result !== $key) ? $result : null;
90
    }
91
92 4
    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 4
        if (! $this->isDatabaseAlreadyMigrated()) {
98
            return null;
99
        }
100
101 4
        if (!isset($this->databaseTranslator)) {
102 4
            $this->databaseTranslator = app(DatabaseTranslator::class);
103
        }
104
105 4
        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 4
    private function isDatabaseAlreadyMigrated()
115
    {
116 4
        if (!is_null($this->isDatabaseAlreadyMigrated)) {
117
            return $this->isDatabaseAlreadyMigrated;
118
        }
119
120 4
        return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines'));
121
    }
122
}
123