Passed
Push — master ( 3885b4...04d5c6 )
by Dāvis
03:56
created

TranslatableRepository::removeTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.4285
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 $em;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
14
    public static $connection;
15
    public static $container;
16
17
    public static $redis;
18
    public static $table;
19
20
    public static $localeArr = [
21
        'lv' => 'lv_LV',
22
        'en' => 'en_US',
23
        'ru' => 'ru_RU',
24
    ];
25
26
    public static function getDefaultLocale()
27
    {
28
        self::init();
29
30
        return self::$container->getParameter('sludio_helper.translatable.default_locale');
31
    }
32
33
    public static function init()
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 5 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
34
    {
35
        global $kernel;
36
37
        if ('AppCache' === get_class($kernel)) {
38
            $kernel = $kernel->getKernel();
39
        }
40
        self::$container = $kernel->getContainer();
41
42
        self::$redis = self::$container->get('snc_redis.'.self::$container->getParameter('sludio_helper.redis.translation'));
43
        self::$table = self::$container->getParameter('sludio_helper.translatable.table');
44
    }
45
46
    public static function getTranslations($class, $id)
47
    {
48
        self::init();
49
        $class = str_replace('Proxies\\__CG__\\', '', $class);
50
        $className = explode('\\', $class);
51
        $className = end($className);
52
53
        $result = self::$redis ? unserialize(self::$redis->get(strtolower($className).':translations:'.$id)) : null;
54
        $checked = self::$redis ? unserialize(self::$redis->get(strtolower($className).':translations:'.$id.':checked')) : null;
55
56
        if (!$result && !$checked) {
57
            $data = Quick::get(new Translation(), false, [
58
                'object_class' => $class,
59
                'foreign_key' => $id,
60
            ], true, ['*']);
61
            foreach ($data as $row) {
62
                $result[$row['locale']][$row['field']] = $row['content'];
63
            }
64
65
            if ($result && self::$redis) {
66
                self::$redis->set(strtolower($className).':translations:'.$id, serialize($result));
67
                self::$redis->set(strtolower($className).':translations:'.$id.':checked', serialize(true));
68
            }
69
        }
70
71
        return $result;
72
    }
73
74
    public static function findByLocale($class, $locale, $content, $field = 'slug', $id = null, $id2 = null)
75
    {
76
        self::init();
77
78
        if (strlen($locale) == 2) {
79
            $locale = self::$localeArr[$locale];
80
        }
81
82
        $where = [
83
            'object_class' => $class,
84
            'locale' => $locale,
85
            'field' => $field,
86
        ];
87
        if ($id) {
88
            $where[] = ['foreign_key <> '.$id];
89
        }
90
        if ($id2) {
91
            $where['foreign_key'] = $id2;
92
        } else {
93
            $where['content'] = $content;
94
        }
95
96
        $result = Quick::get(new Translation(), false, $where, true, ['foreign_key']);
97
98
        return $result;
99
    }
100
101
    public static function updateTranslations($class, $locale, $field, $content, $id = 0)
102
    {
103
        $className = explode('\\', $class);
104
        $className = end($className);
105
        self::init();
106
107
        if (strlen($locale) == 2) {
108
            $locale = self::$localeArr[$locale];
109
        }
110
111
        $update = 1;
112
        if (!$id) {
113
            $id = Quick::findNextIdExt(new $class(), self::$em);
114
            $update = 0;
115
        } else {
116
            $update = (int)self::findByLocale($class, $locale, $content, $field, null, $id);
117
        }
118
119
        $content = trim($content) != '' ? $content : null;
120
121
        $translation = new Translation();
122
        $translation->setField($field)
123
            ->setForeignKey($id)
124
            ->setLocale($locale)
125
            ->setObjectClass($class)
126
            ->setContent($content);
127
128
        if ($update === 0) {
129
            Quick::persist($translation);
130
        } else {
131
            $where = [
132
                'field' => $field,
133
                'foreign_key' => $id,
134
                'object_class' => $class,
135
            ];
136
            $tId = Quick::get(new Translation(), true, $where);
137
            Quick::update($tId, $translation);
138
        }
139
140
        if (self::$redis) {
141
            self::$redis->del(strtolower($className).':translations:'.$id);
142
            self::$redis->del(strtolower($className).':translations:'.$id.':ckecked');
143
        }
144
    }
145
146
    public static function removeTranslations($object)
147
    {
148
        self::init();
149
        $class = get_class($object);
150
        $id = $object->getId();
151
152
        $where = [
153
            'object_class' => $class,
154
            'foreign_key' => $id,
155
        ];
156
        Quick::delete(new Translation(), $where);
157
    }
158
159
    public static function getAllTranslations()
160
    {
161
        self::init();
162
163
        $data = Quick::get(new Translation(), false, [], true, ['*']);
164
        $result = [];
165
        foreach ($data as $row) {
166
            $result[$row['object_class']][$row['foreign_key']][$row['locale']][$row['field']] = $row['content'];
167
        }
168
169
        if (count($result)) {
170
            foreach ($result as $class => $objects) {
171
                $className = explode('\\', $class);
172
                $className = end($className);
173
                foreach ($objects as $id => $transl) {
174
                    self::$redis->set(strtolower($className).':translations:'.$id, serialize($transl));
175
                    self::$redis->set(strtolower($className).':translations:'.$id.':checked', serialize(true));
176
                }
177
            }
178
        }
179
180
        return $result;
181
    }
182
}
183