Passed
Push — master ( 7c8ae3...3c7bda )
by Dāvis
05:29
created

TranslatableRepository::findByLocale()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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