Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Repository/TranslationRepository.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Repository;
4
5
use DateTime;
6
use Exception;
7
use Kunstmaan\TranslatorBundle\Entity\Translation;
8
use Kunstmaan\TranslatorBundle\Model\Translation as TranslationModel;
9
use Kunstmaan\TranslatorBundle\Model\TextWithLocale;
10
11
/**
12
 * Translator Repository class
13
 */
14
class TranslationRepository extends AbstractTranslatorRepository
15
{
16
    /**
17
     * Get an array of all domains group by locales
18
     *
19
     * @return array array[0] = ["name" => "messages", "locale" => "nl"]
20
     */
21 3
    public function getAllDomainsByLocale()
22
    {
23 3
        return $this->createQueryBuilder('t')
24 3
            ->select('t.locale, t.domain name')
25 3
            ->addGroupBy('t.locale')
26 3
            ->addGroupBy('t.domain')
27 3
            ->getQuery()
28 3
            ->getArrayResult();
29
    }
30
31
    /**
32
     * Get an array of all non disabled translations
33
     *
34
     * @param string $locale
35
     * @param string $domain
36
     *
37
     * @return array
38
     */
39 1
    public function findAllNotDisabled($locale, $domain = null)
40
    {
41 1
        $qb = $this->createQueryBuilder('t');
42
        $qb
43 1
            ->select('t')
44 1
            ->where('t.locale = :locale')
45 1
            ->andWhere('t.status != :statusstring')
46 1
            ->setParameter('statusstring', Translation::STATUS_DISABLED)
47 1
            ->setParameter('locale', $locale);
48 1
        if (!\is_null($domain) && !empty($domain)) {
49
            $qb->andWhere('t.domain = :tdomain')
50
                ->setParameter('tdomain', $domain);
51
        }
52
53 1
        return $qb->getQuery()->getResult();
54
    }
55
56
    /**
57
     * @return DateTime|null
58
     */
59 1
    public function getLastChangedTranslationDate()
60
    {
61 1
        $em = $this->getEntityManager();
62
63 1
        $flagNew = \Kunstmaan\TranslatorBundle\Entity\Translation::FLAG_NEW;
64 1
        $flagUpdated = \Kunstmaan\TranslatorBundle\Entity\Translation::FLAG_UPDATED;
65
66
        $sql = <<<EOQ
67
SELECT
68
    MAX(compare) as newestDate,
69
    flag
70
FROM (
71
    SELECT created_at as compare, flag FROM %s
72
    UNION ALL
73
    SELECT updated_at as compare, flag FROM %s) CACHE_CHECK
74
WHERE
75 1
    flag IN ('{$flagUpdated}','{$flagNew}')
76
    GROUP BY flag
77
    HAVING MAX(compare) IS NOT NULL
78
    ORDER BY newestDate DESC
79
EOQ;
80 1
        $table = $em->getClassMetaData('KunstmaanTranslatorBundle:Translation')->getTableName();
81
82 1
        $stmt = $em->getConnection()->prepare(sprintf($sql, $table, $table));
83 1
        $stmt->execute();
84 1
        $result = $stmt->fetch();
85
86 1
        if (is_array($result) && count($result) > 0) {
87 1
            return new \DateTime($result['newestDate']);
88
        }
89
90
        return null;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function resetAllFlags()
97
    {
98
        return $this->createQueryBuilder('t')
99
            ->update('KunstmaanTranslatorBundle:Translation', 't')
100
            ->set('t.flag', 'NULL')
101
            ->getQuery()
102
            ->execute();
103
    }
104
105
    /**
106
     * @param $locales
107
     * @param $domains
108
     *
109
     * @return mixed
110
     */
111 1
    public function getTranslationsByLocalesAndDomains($locales, $domains)
112
    {
113 1
        $em = $this->getEntityManager();
114 1
        $qb = $em->createQueryBuilder();
115
        $qb
116 1
            ->select('t')
117 1
            ->from('KunstmaanTranslatorBundle:Translation', 't')
118 1
            ->andWhere('t.status != :statusstring')
119 1
            ->setParameter('statusstring', Translation::STATUS_DISABLED)
120 1
            ->orderBy('t.domain', 'ASC')
121 1
            ->addOrderBy('t.keyword', 'ASC');
122
123 1
        if (count($locales) > 0) {
124 1
            $qb->andWhere($qb->expr()->in('t.locale', $locales));
125
        }
126
127 1
        if (count($domains) > 0) {
128 1
            $qb->andWhere($qb->expr()->in('t.domain', $domains));
129
        }
130
131
        $result = $qb
132 1
            ->getQuery()
133 1
            ->getResult();
134
135 1
        return $result;
136
    }
137
138
    /**
139
     * @param null $entity
140
     *
141
     * @return mixed
142
     */
143 3
    public function flush($entity = null)
144
    {
145 3
        if ($entity !== null) {
146
            $this->persist($entity);
147
        }
148
149 3
        return $this->getEntityManager()->flush();
150
    }
151
152
    /**
153
     * @param $entity
154
     *
155
     * @return mixed
156
     */
157 3
    public function persist($entity)
158
    {
159 3
        return $this->getEntityManager()->persist($entity);
160
    }
161
162
    /**
163
     * @param TranslationModel $translationModel
164
     *
165
     * @return bool
166
     */
167
    public function isUnique(TranslationModel $translationModel)
168
    {
169
        $qb = $this->createQueryBuilder('t');
170
        $count = $qb->select('COUNT(t.id)')
171
            ->where('t.domain = :domain')
172
            ->andWhere('t.keyword = :keyword')
173
            ->setParameter('domain', $translationModel->getDomain())
174
            ->setParameter('keyword', $translationModel->getKeyword())
175
            ->getQuery()
176
            ->getSingleScalarResult();
177
178
        return $count == 0;
179
    }
180
181
    /**
182
     * @param TranslationModel $translationModel
183
     */
184
    public function createTranslations(TranslationModel $translationModel)
185
    {
186
        $this->getEntityManager()->beginTransaction();
187
188
        try {
189
            // Fetch new translation ID
190
            $translationId = $this->getUniqueTranslationId();
191
            /*
192
             * @var TextWithLocale
193
             */
194
            foreach ($translationModel->getTexts() as $textWithLocale) {
195
                $text = $textWithLocale->getText();
196
                if (empty($text)) {
197
                    continue;
198
                }
199
200
                $translation = new Translation();
201
                $translation
202
                    ->setDomain($translationModel->getDomain())
203
                    ->setKeyword($translationModel->getKeyword())
204
                    ->setTranslationId($translationId)
205
                    ->setLocale($textWithLocale->getLocale())
206
                    ->setText($textWithLocale->getText());
207
                $this->getEntityManager()->persist($translation);
208
            }
209
            $this->getEntityManager()->commit();
210
        } catch (Exception $e) {
211
            $this->getEntityManager()->rollback();
212
        }
213
    }
214
215
    /**
216
     * @param TranslationModel $translationModel
217
     * @param                  $translationId
218
     */
219
    public function updateTranslations(TranslationModel $translationModel, $translationId)
220
    {
221
        $this->getEntityManager()->beginTransaction();
222
223
        try {
224
            /*
225
             * @var TextWithLocale
226
             */
227
            foreach ($translationModel->getTexts() as $textWithLocale) {
228
                if ($textWithLocale->getId()) {
229
                    $translation = $this->find($textWithLocale->getId());
230
                    $translation->setLocale($textWithLocale->getLocale())
231
                        ->setText($textWithLocale->getText());
232
                    $this->getEntityManager()->persist($translation);
233
                } else {
234
                    $text = $textWithLocale->getText();
235
                    if (empty($text)) {
236
                        continue;
237
                    }
238
239
                    $translation = new Translation();
240
                    $translation
241
                        ->setDomain($translationModel->getDomain())
242
                        ->setKeyword($translationModel->getKeyword())
243
                        ->setTranslationId($translationId)
244
                        ->setLocale($textWithLocale->getLocale())
245
                        ->setText($textWithLocale->getText());
246
                    $this->getEntityManager()->persist($translation);
247
                }
248
            }
249
            $this->getEntityManager()->commit();
250
        } catch (Exception $e) {
251
            $this->getEntityManager()->rollback();
252
        }
253
    }
254
255
    /**
256
     * Removes all translations with the given translation id
257
     *
258
     * @param string $translationId
259
     *
260
     * @return mixed
261
     */
262
    public function removeTranslations($translationId)
263
    {
264
        return $this->createQueryBuilder('t')
265
            ->delete()
266
            ->where('t.translationId = :translationId')
267
            ->setParameter('translationId', $translationId)
268
            ->getQuery()
269
            ->execute();
270
    }
271
272
    /**
273
     * @return int
0 ignored issues
show
Should the return type not be object|integer|double|string|array|boolean? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
274
     */
275 3
    public function getUniqueTranslationId()
276
    {
277 3
        $qb = $this->createQueryBuilder('t');
278 3
        $newId = $qb->select('MAX(t.translationId)+1')
279 3
            ->getQuery()
280 3
            ->getSingleScalarResult();
281 3
        if (is_null($newId)) {
282 3
            $newId = 1;
283
        }
284
285 3
        return $newId;
286
    }
287
288
    /**
289
     * @param DateTime $date
290
     * @param string   $domain
291
     *
292
     * @return mixed
293
     */
294 1
    public function findDeprecatedTranslationsBeforeDate(DateTime $date, $domain)
295
    {
296 1
        $qb = $this->createQueryBuilder('t');
297 1
        $result = $qb->select('t')
298 1
            ->where('t.status = :status')
299 1
            ->andWhere('t.domain = :domain')
300 1
            ->andWhere('t.updatedAt < :date')
301 1
            ->setParameter('status', Translation::STATUS_DEPRECATED)
302 1
            ->setParameter('domain', $domain)
303 1
            ->setParameter('date', $date)
304 1
            ->getQuery()
305 1
            ->getResult();
306
307 1
        return $result;
308
    }
309
}
310