Completed
Push — feature/test-php-7-2-in-travis ( c82d8e...0c806e )
by
unknown
04:30
created

EmailTemplateRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\ManagementBundle\Configuration\Repository;
20
21
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Doctrine\Common\Persistence\ManagerRegistry;
23
use Surfnet\StepupMiddleware\ManagementBundle\Configuration\Entity\EmailTemplate;
24
25
final class EmailTemplateRepository extends ServiceEntityRepository
26
{
27
    public function __construct(ManagerRegistry $registry)
28
    {
29
        parent::__construct($registry, EmailTemplate::class);
30
    }
31
32
    /**
33
     * @param string $name
34
     * @param string $preferredLocale
35
     * @param string $fallbackLocale
36
     * @return EmailTemplate|null
37
     */
38
    public function findOneByName($name, $preferredLocale, $fallbackLocale)
39
    {
40
        return $this
41
            ->createQueryBuilder('tpl')
42
            ->where('tpl.name = :name')
43
            ->setParameter('name', $name)
44
            ->addSelect(
45
                'CASE WHEN tpl.locale = :preferredLocale THEN 2
46
                      WHEN tpl.locale = :fallbackLocale THEN 1
47
                      ELSE 0
48
                 END AS HIDDEN localePreference'
49
            )
50
            ->setParameter('preferredLocale', $preferredLocale)
51
            ->setParameter('fallbackLocale', $fallbackLocale)
52
            ->orderBy('localePreference', 'DESC')
53
            ->setMaxResults(1)
54
            ->getQuery()
55
            ->getOneOrNullResult();
56
    }
57
58
    /**
59
     * Removes all email templates.
60
     *
61
     * We hydrate all templates and remove them through the entitymanager so that they get
62
     * removed from the IdentityMap. This to prevent issues when replaying the events, where
63
     * deleting them with a delete query would cause errors due to templates not being found.
64
     */
65
    public function removeAll()
66
    {
67
        $templates = $this->findAll();
68
        $em = $this->getEntityManager();
69
70
        foreach ($templates as $template) {
71
            $em->remove($template);
72
        }
73
74
        $em->flush();
75
76
        unset($templates);
77
    }
78
79
    public function save(EmailTemplate $template)
80
    {
81
        $entityManager = $this->getEntityManager();
82
        $entityManager->persist($template);
83
        $entityManager->flush();
84
    }
85
}
86