Completed
Push — master ( 836903...d6bc82 )
by Ben
04:07
created

SquantoTranslator::isDatabaseAlreadyMigrated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Thinktomorrow\Squanto\Translators;
4
5
use Illuminate\Support\Facades\Schema;
6
use Illuminate\Translation\Translator as LaravelTranslator;
7
8
class SquantoTranslator extends LaravelTranslator implements Translator
9
{
10
    private $databaseTranslator;
11
12
    private $keyAsDefault = true;
13 69
    private $isDatabaseAlreadyMigrated = null;
14
15 69
    public function setKeyAsDefault($keyAsDefault = true)
16 69
    {
17
        $this->keyAsDefault = $keyAsDefault;
18
    }
19
20
    /**
21
     * Get the translation for the given key by following this priority chain:
22
     *
23
     * 1. Get from our cached translations
24
     * 2. Get from database
25
     * 3. Get from the /resources/lang
26
     *
27
     * @param  string $key
28
     * @param  array $replace
29
     * @param  string $locale
30
     * @param bool $fallback
31 21
     * @return string
32
     */
33 21
    public function get($key, array $replace = array(), $locale = null, $fallback = true)
34
    {
35 21
        $locale = $locale ?: $this->getLocale();
36 12
37
        if ($result = $this->getFromCache($key, $replace, $locale, $fallback)) {
38
            return $result;
39 12
        }
40
41
        if ($result = $this->getFromDatabase($key, $replace, $locale, $fallback)) {
42
            return $result;
43 12
        }
44
45 12
        $result = parent::get($key, $replace, $locale, $fallback);
46
47
        return ($this->keyAsDefault || $result !== $key) ? $result : null;
48
    }
49
50
    /**
51
     * Retrieve the translation from the squanto cache.
52
     *
53
     * @param $key
54
     * @param array $replace
55
     * @param null $locale
56 21
     * @return mixed|null
57
     */
58 21
    private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true)
59 21
    {
60
        if (false === strpos($key, 'squanto::')) {
61
            $key = 'squanto::'.$key;
62 21
        }
63
64 21
        $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...
65
66
        return ($result !== $key) ? $result : null;
67 12
    }
68
69 12
    private function getFromDatabase($key, array $replace = array(), $locale = null, $fallback = true)
70 12
    {
71
        /**
72
         * If database tables are not present we will soft ignore this call and delegate to the native
73 12
         */
74
        if(! $this->isDatabaseAlreadyMigrated()) return null;
75
76
        if (!isset($this->databaseTranslator)) {
77
            $this->databaseTranslator = app(DatabaseTranslator::class);
78
        }
79
80
        return $this->databaseTranslator->get($key, $replace, $locale, $fallback);
81
    }
82
83
    /**
84
     * Verify that SQUANTO migrations are already run and present in this environment
85
     * Allow for a soft install
86
     *
87
     * @return null
88
     */
89
    private function isDatabaseAlreadyMigrated()
90
    {
91
        if(!is_null($this->isDatabaseAlreadyMigrated)) return $this->isDatabaseAlreadyMigrated;
92
93
        return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines'));
94
    }
95
}
96