Passed
Push — master ( a9bcb8...9eebd9 )
by Dāvis
02:45
created

TranslatableRepository::getTranslations()   D

Complexity

Conditions 9
Paths 20

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 18
nc 20
nop 2
dl 0
loc 28
rs 4.909
c 2
b 1
f 0
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
18
    public static $localeArr = [
19
        'lv' => 'lv_LV',
20
        'en' => 'en_US',
21
        'ru' => 'ru_RU',
22
    ];
23
24
    public static function init()
25
    {
26
        global $kernel;
27
28
        if ('AppCache' === get_class($kernel)) {
29
            $kernel = $kernel->getKernel();
30
        }
31
        $container = $kernel->getContainer();
32
        self::$defaultLocale = $container->getParameter('sludio_helper.translatable.default_locale');
33
34
        self::$redis = $container->get('snc_redis.'.$container->getParameter('sludio_helper.redis.translation'));
35
        self::$table = $container->getParameter('sludio_helper.translatable.table');
36
    }
37
38
    public static function getTranslations($class, $id)
39
    {
40
        self::init();
41
        $class = str_replace('Proxies\\__CG__\\', '', $class);
42
        $className = explode('\\', $class);
43
        $className = end($className);
44
45
        $result = self::$redis ? unserialize(self::$redis->get(strtolower($className).':translations:'.$id)) : null;
46
        $checked = self::$redis ? unserialize(self::$redis->get(strtolower($className).':translations:'.$id.':checked')) : null;
47
48
        if (!$result && !$checked) {
49
            $data = Quick::get(new Translation(), false, [
50
                'object_class' => $class,
51
                'foreign_key' => $id,
52
            ], true, ['*']);
53
            if ($data) {
54
                foreach ($data as $row) {
55
                    $result[$row['locale']][$row['field']] = $row['content'];
56
                }
57
            }
58
59
            if ($result && self::$redis) {
60
                self::$redis->set(strtolower($className).':translations:'.$id, serialize($result));
61
                self::$redis->set(strtolower($className).':translations:'.$id.':checked', serialize(true));
62
            }
63
        }
64
65
        return $result;
66
    }
67
68
    public static function findByLocale($class, $locale, $content, $field = 'slug', $id = null, $id2 = null)
69
    {
70
        self::init();
71
72
        if (strlen($locale) == 2) {
73
            $locale = self::$localeArr[$locale];
74
        }
75
76
        $where = [
77
            'object_class' => $class,
78
            'locale' => $locale,
79
            'field' => $field,
80
        ];
81
        if ($id) {
82
            $where[] = ['foreign_key <> '.$id];
83
        }
84
        if ($id2) {
85
            $where['foreign_key'] = $id2;
86
        } else {
87
            $where['content'] = $content;
88
        }
89
90
        $result = Quick::get(new Translation(), false, $where, true, ['foreign_key']);
91
92
        return $result;
93
    }
94
95
    public static function updateTranslations($class, $locale, $field, $content, $id = 0)
96
    {
97
        $className = explode('\\', $class);
98
        $className = end($className);
99
        self::init();
100
101
        if (strlen($locale) == 2) {
102
            $locale = self::$localeArr[$locale];
103
        }
104
105
        $update = 1;
106
        if (!$id) {
107
            $id = Quick::findNextIdExt(new $class());
0 ignored issues
show
Bug introduced by
The call to Sludio\HelperBundle\Scri...sitory::findNextIdExt() has too few arguments starting with entityManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            /** @scrutinizer ignore-call */ 
108
            $id = Quick::findNextIdExt(new $class());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
108
            $update = 0;
109
        } else {
110
            $update = (int)self::findByLocale($class, $locale, $content, $field, null, $id);
111
        }
112
113
        $content = trim($content) != '' ? $content : null;
114
115
        $translation = new Translation();
116
        $translation->setField($field)
117
            ->setForeignKey($id)
118
            ->setLocale($locale)
119
            ->setObjectClass($class)
120
            ->setContent($content)
121
        ;
122
123
        if ($update === 0) {
124
            Quick::persist($translation);
125
        } else {
126
            $where = [
127
                'field' => $field,
128
                'foreign_key' => $id,
129
                'object_class' => $class,
130
            ];
131
            $tId = Quick::get(new Translation(), true, $where);
132
            Quick::update($tId, $translation);
133
        }
134
135
        if (self::$redis) {
136
            self::$redis->del(strtolower($className).':translations:'.$id);
137
            self::$redis->del(strtolower($className).':translations:'.$id.':ckecked');
138
        }
139
    }
140
141
    public static function removeTranslations($object)
142
    {
143
        self::init();
144
        $class = get_class($object);
145
        $id = $object->getId();
146
147
        $where = [
148
            'object_class' => $class,
149
            'foreign_key' => $id,
150
        ];
151
        Quick::delete(new Translation(), $where);
152
    }
153
154
    public static function getAllTranslations()
155
    {
156
        self::init();
157
158
        $data = Quick::get(new Translation(), false, [], true, ['*']);
159
        $result = [];
160
        foreach ($data as $row) {
161
            $result[$row['object_class']][$row['foreign_key']][$row['locale']][$row['field']] = $row['content'];
162
        }
163
164
        if (count($result)) {
165
            foreach ($result as $class => $objects) {
166
                $className = explode('\\', $class);
167
                $className = end($className);
168
                foreach ($objects as $id => $transl) {
169
                    self::$redis->set(strtolower($className).':translations:'.$id, serialize($transl));
170
                    self::$redis->set(strtolower($className).':translations:'.$id.':checked', serialize(true));
171
                }
172
            }
173
        }
174
175
        return $result;
176
    }
177
}
178