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

DatabaseTranslator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B get() 0 27 5
1
<?php
2
3
namespace Thinktomorrow\Squanto\Translators;
4
5
use Thinktomorrow\Squanto\Domain\Line;
6
use Thinktomorrow\Squanto\Domain\Page;
7
use Thinktomorrow\Squanto\Services\ConvertToTree;
8
9
class DatabaseTranslator implements Translator
10
{
11
    /**
12
     * Get translation for given key from database.
13
     *
14
     * @param $key
15
     * @param array $replace
16
     * @param null $locale
17
     * @param bool $fallback
18 16
     * @return mixed|null
19
     */
20 25
    public function get($key, array $replace = [], $locale = null, $fallback = true)
21 8
    {
22 9
        if (!$line = Line::findByKey($key)) {
23
24 8
            /**
25 2
             * If no line is requested,
26
             * we check if an entire page is asked for
27
             */
28 13
            if($page = Page::findByKey($key))
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29 2
            {
30 1
                $lines = Line::getValuesByLocaleAndPage($locale, $key);
31 1
                return ConvertToTree::fromFlattened($lines, false);
32 8
            }
33
34 4
            return null;
35
        }
36
37 4
        if (!$value = $line->getValue($locale, $fallback)) {
38 1
            return null;
39
        }
40
41 4
        foreach ($replace as $key => $replacer) {
42 1
            $value = str_replace(':'.$key, $replacer, $value);
43
        }
44
45 4
        return $value;
46
    }
47
}
48