Passed
Push — master ( 988f4e...b3bcb6 )
by Alexander
06:25
created

DatabaseHandler::getLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Translation handler for cached translations in JSON Format
5
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
6
namespace Hokan22\LaravelTranslator\Handler;
7
8
use Hokan22\LaravelTranslator\Models\TranslationIdentifier;
9
use Symfony\Component\Translation\Exception\NotFoundResourceException;
10
11
/**
12
 * Class LocaleHandler
13
 *
14
 * @package  Hokan22\LaravelTranslator\Handler
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
15
 * @author   Alexander Viertel <[email protected]>
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 3
Loading history...
16
 * @license  http://opensource.org/licenses/MIT MIT
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
18
class DatabaseHandler extends DefaultHandler
19
{
20
    /**
21
     * DatabaseHandler constructor.
22
     *
23
     * @param string $locale The locale of the translations
24
     */
25
    public function __construct($locale)
26
    {
27
        parent::__construct($locale);
28
29
        $this->refreshCache();
30
    }
31
32
    /**
33
     * Returns the translation for the given identifier
34
     *
35
     * @param string $identifier    Identifier of the translation
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
36
     * @param string $group         Group for the database query
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 9 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
37
     * @throws NotFoundResourceException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
38
     * @throws TranslationNotFoundException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
39
     * @return string returns the found translation for identifier
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
40
     */
41
    public function getTranslation($identifier, $group)
42
    {
43
        if (isset($this->translations[$identifier])) {
44
            if ($this->translations[$identifier]->translation == null) {
45
                throw new TranslationNotFoundException("The translation for identifier '".$identifier."' and locale '".$this->locale."' could not be found");
46
            }
47
            return $this->translations[$identifier]->translation;
48
        } else {
49
            throw new NotFoundResourceException("The translation identifier '".$identifier."' could not be found");
50
        }
51
    }
52
53
    /**
54
     * Refresh the internal Cache
55
     *
56
     * @return void
57
     */
58
    public function refreshCache()
59
    {
60
        $translations = new TranslationIdentifier();
61
        $translations = $translations->leftJoin('translations', function ($join)
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
62
            {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
63
                $join->on( 'translation_identifiers.id', '=', 'translations.translation_identifier_id')
0 ignored issues
show
Coding Style introduced by
Space after opening parenthesis of function call prohibited
Loading history...
64
                     ->where('locale', $this->locale);
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 20 spaces but found 21
Loading history...
65
            }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace indented incorrectly; expected 8 spaces, found 12
Loading history...
66
            )->get();
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
67
68
        foreach ($translations as $identifier) {
69
            $this->translations[$identifier->identifier] = $identifier;
70
        }
71
    }
72
73
    /**
74
     * Get all translation of $group
75
     *
76
     * @param string $group Group of the translations to return
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
77
     * @return array Translations of the given group
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
78
     */
79
    public function getAllTranslations($group = 'default')
80
    {
81
        $return = [];
82
        foreach (collect($this->translations)->where('group', $group) as $key => $translation) {
83
            if ($translation->translation == null) {
84
                $return[$key] = $translation->identifier;
85
            } else {
86
                $return[$key] = $translation->translation;
87
            }
88
        }
89
        return $return;
90
    }
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter "$identifier" missing
Loading history...
93
     * Get the DB ID of the Identifier
94
     *
95
     * @param $identifier
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
96
     * @throws NotFoundResourceException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
97
     * @return integer
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
98
     */
99
    function getDatabaseID($identifier)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
        if(isset($this->translations[$identifier])) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
102
            return $this->translations[$identifier]->id;
103
        } else {
104
            throw new NotFoundResourceException("The translation identifier '".$identifier."' could not be found");
105
        }
106
    }
107
}