Passed
Push — master ( 20265b...bdd2be )
by Dāvis
03:06
created

TranslatableRepository   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 175
rs 10
wmc 28

7 Methods

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