CacheJSONHandler::getTranslation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * Translation handler for cached translations in JSON Format
5
 */
6
namespace Hokan22\LaravelTranslator\Handler;
7
8
use Hokan22\LaravelTranslator\TranslatorFacade;
9
10
/**
11
 * Class CacheJSONHandler
12
 *
13
 * @package  Hokan22\LaravelTranslator\Handler
14
 * @author   Alexander Viertel <[email protected]>
15
 * @license  http://opensource.org/licenses/MIT MIT
16
 */
17
class CacheJSONHandler extends DefaultHandler
18
{
19
    /**
20
     * DatabaseHandler constructor.
21
     *
22
     * @param string $locale The locale of the translations
23
     * @throws TranslationCacheNotFound
24
     */
25
    public function __construct($locale) {
26
        parent::__construct($locale);
27
28
        $this->refreshCache();
29
    }
30
31
    /**
32
     * Returns the translation for the given identifier
33
     *
34
     * @param string $identifier Identifier for the database query
35
     * @param string $group
36
     * @throws TranslationNotInCacheException
37
     * @return string returns the found translation for locale and identifier
38
     */
39
    public function getTranslation($identifier, $group = 'default') {
40
        // NOTE: This should never trigger the addition of the identifier to the database!
41
        // Because the cache will not be updated automatically.
42
        // the same identifier will not be found twice in the cache, which will result in a duplicate key sql error.
43
        if (isset($this->translations[$group][$identifier])) {
44
            return $this->translations[$group][$identifier];
45
        } else {
46
            throw new TranslationNotInCacheException("The translation identifier '" . $identifier . "' could not be found in Cache");
47
        }
48
    }
49
50
    /**
51
     * Refresh the internal Cache
52
     *
53
     * @param string $group
54
     * @throws TranslationCacheNotFound
55
     */
56
    public function refreshCache($group = 'default') {
57
        $locale_dir = TranslatorFacade::getConfigValue('cache_path') . $this->locale;
58
59
        try {
60
            $trans_identifier = json_decode(file_get_contents($locale_dir . '/' . $group . '.json'), true);
61
        } catch (\ErrorException $e) {
62
            throw new TranslationCacheNotFound("The Translation cache file '" . $locale_dir . '/' . $group . '.json' . "' could not be found!");
63
        }
64
65
        $this->translations[$group] = $trans_identifier;
66
    }
67
68
    /**
69
     * Get all translation of $group
70
     *
71
     * @param string $group Group of the translations to return
72
     * @return array|mixed Translations of the given group
73
     */
74
    public function getAllTranslations($group) {
75
        return $this->translations[$group];
76
    }
77
}