Passed
Push — master ( 303f89...eaed60 )
by Fran
12:51 queued 11s
created

CustomTranslateExtension::getTokenParsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
namespace PSFS\base\extension;
3
4
use PSFS\base\Logger;
5
use PSFS\base\Security;
6
use PSFS\base\types\helpers\I18nHelper;
7
use PSFS\base\types\traits\SingletonTrait;
8
9
/**
10
 * Class CustomTranslateExtension
11
 * @package PSFS\base\extension
12
 */
13
class CustomTranslateExtension extends \Twig_Extension {
14
    use SingletonTrait;
15
16
    const CUSTOM_LOCALE_SESSION_KEY = '__PSFS_CUSTOM_LOCALE_KEY__';
17
18
    protected static $translations = [];
19
    protected static $locale = 'es_ES';
20
21
    /**
22
     * @param string $custom_key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $custom_key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
23
     * @param bool $force_reload
24
     */
25 1
    protected static function checkLoad($custom_key = null, $force_reload = false) {
26 1
        if($force_reload) self::dropInstance();
27 1
        self::$locale = I18nHelper::extractLocale('es_ES');
28 1
        $custom_key = $custom_key ?: Security::getInstance()->getSessionKey(self::CUSTOM_LOCALE_SESSION_KEY);
29 1
        if(null !== $custom_key) {
30
            Logger::log('[' . self::class . '] Custom key detected: ' . $custom_key, LOG_INFO);
31
            $filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', $custom_key, self::$locale . '.json']);
32
            if(file_exists($filename)) {
33
                Logger::log('[' . self::class . '] Custom locale detected: ' . $custom_key . ' [' . self::$locale . ']', LOG_INFO);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
34
                self::$translations = json_decode(file_get_contents($filename), true);
35
            }
36
        }
37 1
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function getTokenParsers()
43
    {
44 1
        return array(new \Twig_Extensions_TokenParser_Trans());
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getFilters()
51
    {
52
        return array(
53 1
            new \Twig_SimpleFilter('trans', function($message) {
54 1
                return self::_($message);
55 1
            }),
56
        );
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function getName()
63
    {
64 1
        return 'PSFSi18n';
65
    }
66
67
    /**
68
     * @param $message
69
     * @param string $custom_key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $custom_key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
70
     * @param bool $force_reload
71
     * @return mixed|string
72
     */
73 1
    public static function _($message, $custom_key = null, $force_reload = false) {
74 1
        self::checkLoad($custom_key, $force_reload);
75 1
        if(array_key_exists($message, self::$translations)) {
76
            return self::$translations[$message];
77
        } else {
78 1
            return gettext($message);
79
        }
80
    }
81
}