Passed
Push — master ( 151800...a77de0 )
by Dāvis
03:07
created

TranslatableRepository::init()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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