Passed
Push — master ( eb2b9c...2ce42f )
by Dāvis
05:18 queued 02:31
created

TranslatableRepository::getAllTranslations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 3
b 0
f 0
nc 3
nop 0
dl 0
loc 8
rs 9.4285
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\Translation;
7
use Sludio\HelperBundle\Translatable\Entity\BaseEntity;
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 getLocaleVar($locale)
24
    {
25
        return isset(self::$localeArr[$locale]) ? self::$localeArr[$locale] : $locale;
26
    }
27
28
    public static function getDefaultLocale()
29
    {
30
        self::init();
31
32
        return self::$defaultLocale;
33
    }
34
35
    public static function init($class = null, &$className = null)
36
    {
37
        if ($class) {
38
            $class = explode('\\', $class);
39
            $className = end($class);
40
        }
41
        if (self::$redis) {
42
            return;
43
        }
44
        global $kernel;
45
46
        if ('AppCache' === get_class($kernel)) {
47
            $kernel = $kernel->getKernel();
48
        }
49
        $container = $kernel->getContainer();
50
        self::$entityManager = $container->get('doctrine')->getManager();
51
        self::$defaultLocale = $container->getParameter('sludio_helper.translatable.default_locale');
52
53
        self::$redis = $container->get('snc_redis.'.$container->getParameter('sludio_helper.redis.translation'));
54
        self::$table = $container->getParameter('sludio_helper.translatable.table');
55
    }
56
57
    private static function getFromRedis($key, &$result, &$checked)
58
    {
59
        if (self::$redis !== null) {
60
            $result = unserialize(self::$redis->get(self::tKey($key)), ['allowed_classes' => false]);
61
            $checked = unserialize(self::$redis->get(self::cKey($key)), ['allowed_classes' => false]);
62
        }
63
    }
64
65
    private static function setToRedis($key, $result)
66
    {
67
        if (!empty($result) && self::$redis !== null) {
68
            self::$redis->set(self::tKey($key), serialize($result));
69
            self::$redis->set(self::cKey($key), serialize(true));
70
        }
71
    }
72
73
    private static function delFromRedis($key)
74
    {
75
        if (self::$redis !== null) {
76
            self::$redis->del(self::tKey($key));
77
            self::$redis->del(self::cKey($key));
78
        }
79
    }
80
81
    private static function tKey(&$key)
82
    {
83
        return $key.':translations';
84
    }
85
86
    private static function cKey(&$key)
87
    {
88
        return $key.':checked';
89
    }
90
91
    public static function getTranslations($class, $id, $skip = false)
92
    {
93
        $class = str_replace('Proxies\\__CG__\\', '', $class);
94
        self::init($class, $className);
95
96
        $key = strtolower($className).':translations:'.$id;
97
        $result = [];
98
        $checked = false;
99
        if ($skip === false) {
100
            self::getFromRedis($key, $result, $checked);
101
        } else {
102
            self::delFromRedis($key);
103
        }
104
105
        if (empty($result) && !$checked) {
106
            $data = Quick::get(new Translation(), false, [
107
                'object_class' => $class,
108
                'foreign_key' => $id,
109
            ], ['*']);
110
            if ($data !== null) {
111
                foreach ($data as $row) {
112
                    $result[$row['locale']][$row['field']] = $row['content'];
113
                }
114
            }
115
116
            self::setToRedis($key, $result);
117
        }
118
119
        return $result;
120
    }
121
122
    public static function findByLocale($class, $locale, $content, $field = 'slug', $notId = null, $isId = null)
123
    {
124
        self::init();
125
        $locale = self::getLocaleVar($locale ?: self::getDefaultLocale());
126
127
        $where = [
128
            'object_class' => $class,
129
            'locale' => $locale,
130
            'field' => $field,
131
        ];
132
        if ($notId) {
133
            $where[] = ['foreign_key <> '.$notId];
134
        }
135
        if ($isId) {
136
            $where['foreign_key'] = $isId;
137
        } else {
138
            $where['content'] = $content;
139
        }
140
141
        return Quick::get(new Translation(), false, $where, ['foreign_key']);
142
    }
143
144
    public static function updateTranslations($class, $locale, $field, $content, $id = 0)
145
    {
146
        self::init($class, $className);
147
        $locale = self::getLocaleVar($locale);
148
149
        if (!$id) {
150
            $id = Quick::findNextIdExt(self::$entityManager->getMetadataFactory()->getMetadataFor($class));
151
            $update = 0;
152
        } else {
153
            $update = (int)self::findByLocale($class, $locale, $content, $field, null, $id);
154
        }
155
156
        $content = trim($content) != '' ? $content : null;
157
158
        $translation = new Translation();
159
        $translation->setField($field)
160
            ->setForeignKey($id)
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type true; however, parameter $foreignKey of Sludio\HelperBundle\Tran...lation::setForeignKey() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

160
            ->setForeignKey(/** @scrutinizer ignore-type */ $id)
Loading history...
161
            ->setLocale($locale)
162
            ->setObjectClass($class)
163
            ->setContent($content)
164
        ;
165
166
        if ($update === 0) {
167
            Quick::persist($translation);
168
        } else {
169
            $where = [
170
                'field' => $field,
171
                'foreign_key' => $id,
172
                'object_class' => $class,
173
                'locale' => $locale,
174
            ];
175
            $tId = Quick::get(new Translation(), true, $where);
176
            Quick::update($tId, $translation);
177
        }
178
179
        self::getTranslations($class, $id, true);
180
    }
181
182
    public static function removeTranslations(BaseEntity $object)
183
    {
184
        $class = get_class($object);
185
        self::init($class, $className);
186
        $id = $object->getId();
187
188
        $where = [
189
            'object_class' => $class,
190
            'foreign_key' => $id,
191
        ];
192
        Quick::delete(new Translation(), $where);
193
        $key = strtolower($className).':translations:'.$id;
194
        self::delFromRedis($key);
195
    }
196
197
    public static function getAllTranslations()
198
    {
199
        self::init();
200
        $classes = Quick::get(new Translation(), false, [], ['object_class'], null, ['MODE' => 'DISTINCT']);
201
        foreach ($classes as $class) {
202
            $ids = Quick::get(new Translation(), false, ['object_class' => $class], ['foreign_key'], null, ['MODE' => 'DISTINCT']);
203
            foreach ($ids as $id) {
204
                self::getTranslations($class, $id, true);
205
            }
206
        }
207
    }
208
}
209