LangTranslateAdapter::storeToCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     LangTranslateAdapter
7
 *
8
 */
9
class LangTranslateAdapter extends i18nSSLegacyAdapter {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
    /**
12
     *
13
     * @param array|string $messageId
14
     * @param string       $locale
15
     *
16
     * @return string
17
     */
18
    public function translate($messageId, $locale = null) {
19
        if (class_exists('LangEntity') && class_exists('LangModule')) {
20
            $query = DB::query("SHOW TABLES LIKE 'LangEntity'");
21
22
            if (count($query->column()) > 0) {
23
                if ($result = $this->getFromCache($messageId, $locale)) {
0 ignored issues
show
Bug introduced by
It seems like $messageId defined by parameter $messageId on line 18 can also be of type array; however, LangTranslateAdapter::getFromCache() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
24
                    return $result;
25
                }
26
27
                if (! is_array($messageId)) {
28
                    try {
29
                        $entity = LangEntity::getByNamespace($messageId);
30
                    } catch (SS_DatabaseException $ex) {
31
                        return parent::translate($messageId, $locale);
32
                    }
33
34
                    if ($entity) {
35
                        $this->storeToCache($messageId, $entity->Value, $locale);
36
37
                        return $entity->Value;
38
                    }
39
                }
40
            }
41
        }
42
43
        return parent::translate($messageId, $locale);
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function toString() {
50
        return static::class;
51
    }
52
53
    /**
54
     * @param string $namespace
55
     * @param string $locale
56
     *
57
     * @return string|false
58
     */
59
    public function getFromCache($namespace, $locale = null) {
60
        $namespace = static::filterCacheKey($namespace);
61
62
        if (! is_null($locale)) {
63
            $locale = strtoupper(explode('_', $locale)[0]);
64
        }
65
66
        $cacheKey = sprintf('_%s_%s', $namespace, $locale);
67
        $cache = SS_Cache::factory(LangEditor::class);
68
        $result = $cache->load($cacheKey);
69
70
        if ($result) {
71
            return $result;
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * @param string $namespace
79
     * @param string $value
80
     * @param string $locale
81
     *
82
     * @return void
83
     */
84
    public function storeToCache($namespace, $value, $locale = null) {
85
        $namespace = static::filterCacheKey($namespace);
86
87
        if (! is_null($locale)) {
88
            $locale = strtoupper(explode('_', $locale)[0]);
89
        }
90
91
        $cacheKey = sprintf('_%s_%s', $namespace, $locale);
92
        $cache = SS_Cache::factory(LangEditor::class);
93
        $cache->save($value, $cacheKey);
94
    }
95
96
    /**
97
     * @param string $value
98
     *
99
     * @return string
100
     */
101
    public static function filterCacheKey($value) {
102
        return strtolower(trim(preg_replace('~[^a-zA-Z0-9_]+~u', '_', LangFulltextBooleanFilter::convertForeignToLatin($value))));
103
    }
104
105
}