Completed
Push — 5.6 ( 2839de...185d99 )
by Jeroen
11:17
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
10
/**
11
 * Translator Repository class
12
 */
13
class TranslationRepository extends AbstractTranslatorRepository
14
{
15
    /**
16
     * Get an array of all domains group by locales
17
     *
18
     * @return array array[0] = ["name" => "messages", "locale" => "nl"]
19
     */
20 3
    public function getAllDomainsByLocale()
21
    {
22 3
        return $this->createQueryBuilder('t')
23 3
            ->select('t.locale, t.domain name')
24 3
            ->addGroupBy('t.locale')
25 3
            ->addGroupBy('t.domain')
26 3
            ->getQuery()
27 3
            ->getArrayResult();
28
    }
29
30
    /**
31
     * Get an array of all non disabled translations
32
     *
33
     * @param string $locale
34
     * @param string $domain
35
     *
36
     * @return array
37
     */
38 1
    public function findAllNotDisabled($locale, $domain = null)
39
    {
40 1
        $qb = $this->createQueryBuilder('t');
41
        $qb
42 1
            ->select('t')
43 1
            ->where('t.locale = :locale')
44 1
            ->andWhere('t.status != :statusstring')
45 1
            ->setParameter('statusstring', Translation::STATUS_DISABLED)
46 1
            ->setParameter('locale', $locale);
47 1
        if (!\is_null($domain) && !empty($domain)) {
48
            $qb->andWhere('t.domain = :tdomain')
49
                ->setParameter('tdomain', $domain);
50
        }
51
52 1
        return $qb->getQuery()->getResult();
53
    }
54
55
    /**
56
     * @return DateTime|null
57
     */
58 1
    public function getLastChangedTranslationDate()
59
    {
60 1
        $em = $this->getEntityManager();
61
62 1
        $flagNew = \Kunstmaan\TranslatorBundle\Entity\Translation::FLAG_NEW;
63 1
        $flagUpdated = \Kunstmaan\TranslatorBundle\Entity\Translation::FLAG_UPDATED;
64
65
        $sql = <<<EOQ
66 1
SELECT
67
    MAX(compare) as newestDate,
68
    flag
69
FROM (
70
    SELECT created_at as compare, flag FROM %s
71
    UNION ALL
72
    SELECT updated_at as compare, flag FROM %s) CACHE_CHECK
73
WHERE
74 1
    flag IN ('{$flagUpdated}','{$flagNew}')
75
    GROUP BY flag
76
    HAVING MAX(compare) IS NOT NULL
77
    ORDER BY newestDate DESC
78
EOQ;
79 1
        $table = $em->getClassMetadata('KunstmaanTranslatorBundle:Translation')->getTableName();
80
81 1
        $stmt = $em->getConnection()->prepare(sprintf($sql, $table, $table));
82 1
        $stmt->execute();
83 1
        $result = $stmt->fetch();
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Statement::fetch() has been deprecated with message: Use fetchNumeric(), fetchAssociative() or fetchOne() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
85 1
        if (\is_array($result) && \count($result) > 0) {
86 1
            return new \DateTime($result['newestDate']);
87
        }
88
89
        return null;
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function resetAllFlags()
96
    {
97
        return $this->createQueryBuilder('t')
98
            ->update('KunstmaanTranslatorBundle:Translation', 't')
99
            ->set('t.flag', 'NULL')
100
            ->getQuery()
101
            ->execute();
102
    }
103
104
    /**
105
     * @param $locales
106
     * @param $domains
107
     *
108
     * @return mixed
109
     */
110 1
    public function getTranslationsByLocalesAndDomains($locales, $domains)
111
    {
112 1
        $em = $this->getEntityManager();
113 1
        $qb = $em->createQueryBuilder();
114
        $qb
115 1
            ->select('t')
116 1
            ->from('KunstmaanTranslatorBundle:Translation', 't')
117 1
            ->andWhere('t.status != :statusstring')
118 1
            ->setParameter('statusstring', Translation::STATUS_DISABLED)
119 1
            ->orderBy('t.domain', 'ASC')
120 1
            ->addOrderBy('t.keyword', 'ASC');
121
122 1
        if (\count($locales) > 0) {
123 1
            $qb->andWhere($qb->expr()->in('t.locale', $locales));
124
        }
125
126 1
        if (\count($domains) > 0) {
127 1
            $qb->andWhere($qb->expr()->in('t.domain', $domains));
128
        }
129
130
        return $qb
131 1
            ->getQuery()
132 1
            ->getResult();
133
    }
134
135
    /**
136
     * @param null $entity
137
     *
138
     * @return mixed
139
     */
140 3
    public function flush($entity = null)
141
    {
142 3
        if ($entity !== null) {
143
            $this->persist($entity);
144
        }
145
146 3
        return $this->getEntityManager()->flush();
147
    }
148
149
    /**
150
     * @param $entity
151
     *
152
     * @return mixed
153
     */
154 3
    public function persist($entity)
155
    {
156 3
        return $this->getEntityManager()->persist($entity);
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function isUnique(TranslationModel $translationModel)
163
    {
164
        $qb = $this->createQueryBuilder('t');
165
        $count = $qb->select('COUNT(t.id)')
166
            ->where('t.domain = :domain')
167
            ->andWhere('t.keyword = :keyword')
168
            ->setParameter('domain', $translationModel->getDomain())
169
            ->setParameter('keyword', $translationModel->getKeyword())
170
            ->getQuery()
171
            ->getSingleScalarResult();
172
173
        return $count == 0;
174
    }
175
176
    public function createTranslations(TranslationModel $translationModel)
177
    {
178
        $this->getEntityManager()->beginTransaction();
179
180
        try {
181
            // Fetch new translation ID
182
            $translationId = $this->getUniqueTranslationId();
183
            /*
184
             * @var TextWithLocale
185
             */
186
            foreach ($translationModel->getTexts() as $textWithLocale) {
187
                $text = $textWithLocale->getText();
188
                if (empty($text)) {
189
                    continue;
190
                }
191
192
                $translation = new Translation();
193
                $translation
194
                    ->setDomain($translationModel->getDomain())
195
                    ->setKeyword($translationModel->getKeyword())
196
                    ->setTranslationId($translationId)
197
                    ->setLocale($textWithLocale->getLocale())
198
                    ->setText($textWithLocale->getText());
199
                $this->getEntityManager()->persist($translation);
200
            }
201
            $this->getEntityManager()->commit();
202
        } catch (Exception $e) {
203
            $this->getEntityManager()->rollback();
204
        }
205
    }
206
207
    /**
208
     * @param $translationId
209
     */
210
    public function updateTranslations(TranslationModel $translationModel, $translationId)
211
    {
212
        $this->getEntityManager()->beginTransaction();
213
214
        try {
215
            /*
216
             * @var TextWithLocale
217
             */
218
            foreach ($translationModel->getTexts() as $textWithLocale) {
219
                if ($textWithLocale->getId()) {
220
                    $translation = $this->find($textWithLocale->getId());
221
                    $translation->setLocale($textWithLocale->getLocale())
222
                        ->setText($textWithLocale->getText());
223
                    $this->getEntityManager()->persist($translation);
224
                } else {
225
                    $text = $textWithLocale->getText();
226
                    if (empty($text)) {
227
                        continue;
228
                    }
229
230
                    $translation = new Translation();
231
                    $translation
232
                        ->setDomain($translationModel->getDomain())
233
                        ->setKeyword($translationModel->getKeyword())
234
                        ->setTranslationId($translationId)
235
                        ->setLocale($textWithLocale->getLocale())
236
                        ->setText($textWithLocale->getText());
237
                    $this->getEntityManager()->persist($translation);
238
                }
239
            }
240
            $this->getEntityManager()->commit();
241
        } catch (Exception $e) {
242
            $this->getEntityManager()->rollback();
243
        }
244
    }
245
246
    /**
247
     * Removes all translations with the given translation id
248
     *
249
     * @param string $translationId
250
     *
251
     * @return mixed
252
     */
253
    public function removeTranslations($translationId)
254
    {
255
        return $this->createQueryBuilder('t')
256
            ->delete()
257
            ->where('t.translationId = :translationId')
258
            ->setParameter('translationId', $translationId)
259
            ->getQuery()
260
            ->execute();
261
    }
262
263
    /**
264
     * @return int
265
     */
266 3
    public function getUniqueTranslationId()
267
    {
268 3
        $qb = $this->createQueryBuilder('t');
269 3
        $newId = $qb->select('MAX(t.translationId)+1')
270 3
            ->getQuery()
271 3
            ->getSingleScalarResult();
272 3
        if (\is_null($newId)) {
273 3
            $newId = 1;
274
        }
275
276 3
        return $newId;
277
    }
278
279
    /**
280
     * @param string $domain
281
     *
282
     * @return mixed
283
     */
284 1
    public function findDeprecatedTranslationsBeforeDate(DateTime $date, $domain)
285
    {
286 1
        $qb = $this->createQueryBuilder('t');
287 1
        $result = $qb->select('t')
288 1
            ->where('t.status = :status')
289 1
            ->andWhere('t.domain = :domain')
290 1
            ->andWhere('t.updatedAt < :date')
291 1
            ->setParameter('status', Translation::STATUS_DEPRECATED)
292 1
            ->setParameter('domain', $domain)
293 1
            ->setParameter('date', $date)
294 1
            ->getQuery()
295 1
            ->getResult();
296
297 1
        return $result;
298
    }
299
}
300