Completed
Push — master ( d6bc82...9d9205 )
by Ben
08:50
created

SquantoTranslator::isDatabaseAlreadyMigrated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
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 46
    private $isDatabaseAlreadyMigrated = null;
14
15 72
    public function setKeyAsDefault($keyAsDefault = true)
16 46
    {
17 26
        $this->keyAsDefault = $keyAsDefault;
18 26
    }
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 14
     * @return string
32
     */
33 21
    public function get($key, array $replace = array(), $locale = null, $fallback = true)
34
    {
35 21
        $locale = $locale ?: $this->getLocale();
36 8
37 7
        if ($result = $this->getFromCache($key, $replace, $locale, $fallback)) {
38 4
            return $result;
39 8
        }
40
41 4
        if ($result = $this->getFromDatabase($key, $replace, $locale, $fallback)) {
42
            return $result;
43 8
        }
44
45 12
        $result = parent::get($key, $replace, $locale, $fallback);
46
47 4
        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 14
     * @return mixed|null
57
     */
58 21
    private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true)
59 14
    {
60 7
        if (false === strpos($key, 'squanto::')) {
61 7
            $key = 'squanto::'.$key;
62 14
        }
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 7
        return ($result !== $key) ? $result : null;
67 8
    }
68
69 12
    private function getFromDatabase($key, array $replace = array(), $locale = null, $fallback = true)
70 8
    {
71
        /**
72
         * If database tables are not present we will soft ignore this call and delegate to the native
73 8
         */
74 4
        if (! $this->isDatabaseAlreadyMigrated()) {
75
            return null;
76
        }
77
78 4
        if (!isset($this->databaseTranslator)) {
79 4
            $this->databaseTranslator = app(DatabaseTranslator::class);
80
        }
81
82 4
        return $this->databaseTranslator->get($key, $replace, $locale, $fallback);
83
    }
84
85
    /**
86
     * Verify that SQUANTO migrations are already run and present in this environment
87
     * Allow for a soft install
88
     *
89
     * @return null
90
     */
91 4
    private function isDatabaseAlreadyMigrated()
92
    {
93 4
        if (!is_null($this->isDatabaseAlreadyMigrated)) {
94
            return $this->isDatabaseAlreadyMigrated;
95
        }
96
97 4
        return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines'));
98
    }
99
}
100