Passed
Push — master ( 9bcfb0...988f4e )
by Alexander
06:53
created

DatabaseHandler::getTranslation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
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
 * @category TranslatorHandler
15
 * @package  Hokan22\LaravelTranslator\Handler
16
 * @author   Alexander Viertel <[email protected]>
17
 * @license  http://opensource.org/licenses/MIT MIT
18
 * @link     https://github.com/Hokan22/laravel-translator
19
 */
20
class DatabaseHandler implements HandlerInterface
21
{
22
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
     * @var string          $locale         The locale to translate to
24
     * @var array|array[]   $translations   Array with the identifiers as keys and the Texts object as value
25
     */
26
    protected $locale, $translations;
27
28
    /**
29
     * DatabaseHandler constructor.
30
     *
31
     * @param string $locale The locale of the translations
32
     */
33
    public function __construct($locale)
34
    {
35
        $this->locale = $locale;
36
37
        $this->refreshCache();
38
    }
39
40
    /**
41
     * Returns the currently set locale
42
     *
43
     * @return string Return the locale to translate to
44
     */
45
    public function getLocale()
46
    {
47
        return $this->locale;
48
    }
49
50
    /**
51
     * Returns the translation for the given identifier
52
     *
53
     * @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...
54
     * @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...
55
     * @throws NotFoundResourceException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
56
     * @throws TranslationNotFoundException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
57
     * @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...
58
     */
59
    public function getTranslation($identifier, $group)
60
    {
61
62
        if (isset($this->translations[$identifier])) {
63
            if ($this->translations[$identifier]->translation == null) {
64
                throw new TranslationNotFoundException("The translation for identifier '".$identifier."' and locale '".$this->locale."' could not be found");
65
            }
66
            return $this->translations[$identifier]->translation;
67
        } else {
68
            throw new NotFoundResourceException("The translation identifier '".$identifier."' could not be found");
69
        }
70
    }
71
72
    /**
73
     * Refresh the internal Cache
74
     *
75
     * @return void
76
     */
77
    public function refreshCache()
78
    {
79
        // Get all Texts with translations for the given locale
80
        $translations = new TranslationIdentifier();
81
        $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...
82
            {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
83
                $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...
84
                     ->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...
85
            }
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...
86
            )->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...
87
88
        foreach ($translations as $identifier) {
89
            $this->translations[$identifier->identifier] = $identifier;
90
        }
91
    }
92
93
    /**
94
     * Get all translation of $group
95
     *
96
     * @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...
97
     * @return array|mixed 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...
98
     */
99
    public function getAllTranslations($group = 'default')
100
    {
101
        $return = [];
102
        foreach (collect($this->translations)->where('group', $group) as $key => $translation) {
103
            if ($translation->translation == null) {
104
                $return[$key] = $translation->identifier;
105
            } else {
106
                $return[$key] = $translation->translation;
107
            }
108
        }
109
        return $return;
110
    }
111
112
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter "$identifier" missing
Loading history...
113
     * Get the DB ID of the Identifier
114
     *
115
     * @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...
116
     * @throws NotFoundResourceException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
117
     * @return integer
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
118
     */
119
    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...
120
    {
121
        if(isset($this->translations[$identifier])) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
122
            return $this->translations[$identifier]->id;
123
        } else {
124
            throw new NotFoundResourceException("The translation identifier '".$identifier."' could not be found");
125
        }
126
    }
127
}