Passed
Push — master ( a51f35...a51f35 )
by Dāvis
05:00
created

TranslatableRepository   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 9
Bugs 2 Features 0
Metric Value
dl 0
loc 203
rs 8.8
c 9
b 2
f 0
wmc 36

13 Methods

Rating   Name   Duplication   Size   Complexity  
A removeTranslations() 0 13 1
A delFromRedis() 0 5 2
B getTranslations() 0 30 6
A getFromRedis() 0 5 2
A init() 0 20 4
A tKey() 0 3 1
A setToRedis() 0 5 3
A findByLocale() 0 20 4
B updateTranslations() 0 38 4
A getDefaultLocale() 0 5 1
A cKey() 0 3 1
B getAllTranslations() 0 12 5
A getLocaleVar() 0 3 2
1
<?php
2
3
namespace Sludio\HelperBundle\Translatable\Repository;
4
5
use Sludio\HelperBundle\Script\Repository\QuickInsertRepository as Quick;
6
use Sludio\HelperBundle\Translatable\Entity\BaseEntity;
7
use Sludio\HelperBundle\Translatable\Entity\Translation;
8
9
class TranslatableRepository
10
{
11
    public static $defaultLocale;
12
13
    public static $redis;
14
    public static $table;
15
    public static $entityManager;
16
17
    public static $localeArr = [
18
        'lv' => 'lv_LV',
19
        'en' => 'en_US',
20
        'ru' => 'ru_RU',
21
    ];
22
23
    public static function updateTranslations($class, $locale, $field, $content, $id = 0)
24
    {
25
        self::init($class, $className);
26
        $locale = self::getLocaleVar($locale);
27
28
        if (\in_array($id, [
29
            0,
30
            null,
31
        ], true)) {
32
            $id = Quick::findNextIdExt(self::$entityManager->getMetadataFactory()->getMetadataFor($class));
33
            $update = 0;
34
        } else {
35
            $update = (int)self::findByLocale($class, $locale, $content, $field, null, $id);
36
        }
37
38
        $content = trim($content) !== '' ? $content : null;
39
40
        $translation = new Translation();
41
        $translation->setField($field)
42
            ->setForeignKey((int)$id)
43
            ->setLocale($locale)
44
            ->setObjectClass($class)
45
            ->setContent($content);
46
47
        if ($update === 0) {
48
            Quick::persist($translation);
49
        } else {
50
            $where = [
51
                'field' => $field,
52
                'foreign_key' => $id,
53
                'object_class' => $class,
54
                'locale' => $locale,
55
            ];
56
            $tId = Quick::get(new Translation(), true, $where);
57
            Quick::update($tId, $translation);
58
        }
59
60
        self::getTranslations($class, $id, true);
61
    }
62
63
    public static function init($class = null, &$className = null)
64
    {
65
        if ($class) {
66
            $class = explode('\\', $class);
67
            $className = end($class);
68
        }
69
        if (self::$redis) {
70
            return;
71
        }
72
        global $kernel;
73
74
        if ('AppCache' === \get_class($kernel)) {
75
            $kernel = $kernel->getKernel();
76
        }
77
        $container = $kernel->getContainer();
78
        self::$entityManager = $container->get('doctrine')->getManager();
79
        self::$defaultLocale = $container->getParameter('sludio_helper.translatable.default_locale');
80
81
        self::$redis = $container->get('snc_redis.'.$container->getParameter('sludio_helper.redis.translation'));
82
        self::$table = $container->getParameter('sludio_helper.translatable.table');
83
    }
84
85
    public static function getLocaleVar($locale)
86
    {
87
        return isset(self::$localeArr[$locale]) ? self::$localeArr[$locale] : $locale;
88
    }
89
90
    public static function findByLocale($class, $locale, $content, $field = 'slug', $notId = null, $isId = null)
91
    {
92
        self::init();
93
        $locale = self::getLocaleVar($locale ?: self::getDefaultLocale());
94
95
        $where = [
96
            'object_class' => $class,
97
            'locale' => $locale,
98
            'field' => $field,
99
        ];
100
        if ($notId) {
101
            $where[] = ['foreign_key <> '.$notId];
102
        }
103
        if ($isId) {
104
            $where['foreign_key'] = $isId;
105
        } else {
106
            $where['content'] = $content;
107
        }
108
109
        return Quick::get(new Translation(), false, $where, ['foreign_key']);
110
    }
111
112
    public static function getDefaultLocale()
113
    {
114
        self::init();
115
116
        return self::$defaultLocale;
117
    }
118
119
    public static function getTranslations($class, $id, $skip = false)
120
    {
121
        $class = str_replace('Proxies\\__CG__\\', '', $class);
122
        self::init($class, $className);
123
124
        $key = strtolower($className).':translations:'.$id;
125
        $result = [];
126
        $checked = false;
127
        if ($skip === false) {
128
            self::getFromRedis($key, $result, $checked);
129
        } else {
130
            self::delFromRedis($key);
131
        }
132
133
        if (empty($result) && !$checked) {
134
            $data = Quick::get(new Translation(), false, [
135
                'object_class' => $class,
136
                'foreign_key' => $id,
137
            ], ['*']);
138
            if ($data !== null) {
139
                /** @var $data array */
140
                foreach ($data as $row) {
141
                    $result[$row['locale']][$row['field']] = $row['content'];
142
                }
143
            }
144
145
            self::setToRedis($key, $result);
146
        }
147
148
        return $result;
149
    }
150
151
    private static function getFromRedis($key, &$result, &$checked)
152
    {
153
        if (self::$redis !== null) {
154
            $result = unserialize(self::$redis->get(self::tKey($key)));
155
            $checked = unserialize(self::$redis->get(self::cKey($key)));
156
        }
157
    }
158
159
    private static function tKey(&$key)
160
    {
161
        return $key.':translations';
162
    }
163
164
    private static function cKey(&$key)
165
    {
166
        return $key.':checked';
167
    }
168
169
    private static function delFromRedis($key)
170
    {
171
        if (self::$redis !== null) {
172
            self::$redis->del(self::tKey($key));
173
            self::$redis->del(self::cKey($key));
174
        }
175
    }
176
177
    private static function setToRedis($key, $result)
178
    {
179
        if (!empty($result) && self::$redis !== null) {
180
            self::$redis->set(self::tKey($key), serialize($result));
181
            self::$redis->set(self::cKey($key), serialize(true));
182
        }
183
    }
184
185
    public static function removeTranslations(BaseEntity $object)
186
    {
187
        $class = \get_class($object);
188
        self::init($class, $className);
189
        $id = $object->getId();
190
191
        $where = [
192
            'object_class' => $class,
193
            'foreign_key' => $id,
194
        ];
195
        Quick::delete(new Translation(), $where);
196
        $key = strtolower($className).':translations:'.$id;
197
        self::delFromRedis($key);
198
    }
199
200
    public static function getAllTranslations()
201
    {
202
        self::init();
203
        $classes = Quick::get(new Translation(), false, [], ['object_class'], null, ['MODE' => 'DISTINCT']);
204
        if (!empty($classes)) {
205
            /** @var $classes array */
206
            foreach ($classes as $class) {
207
                $ids = Quick::get(new Translation(), false, ['object_class' => $class], ['foreign_key'], null, ['MODE' => 'DISTINCT']);
208
                if (!empty($ids)) {
209
                    /** @var $ids array */
210
                    foreach ($ids as $id) {
211
                        self::getTranslations($class, $id, true);
212
                    }
213
                }
214
            }
215
        }
216
    }
217
}
218