Completed
Pull Request — develop (#119)
by Boy
09:02 queued 04:19
created

EmailTemplateRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A removeAll() 0 13 2
A save() 0 6 1
A findOneByName() 0 19 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\ORM\EntityRepository;
22
use Surfnet\StepupMiddleware\ManagementBundle\Configuration\Entity\EmailTemplate;
23
24
final class EmailTemplateRepository extends EntityRepository
25
{
26
    /**
27
     * @param string $name
28
     * @param string $preferredLocale
29
     * @param string $fallbackLocale
30
     * @return \Surfnet\StepupMiddleware\ManagementBundle\Configuration\Entity\EmailTemplate|null
31
     */
32
    public function findOneByName($name, $preferredLocale, $fallbackLocale)
33
    {
34
        return $this
35
            ->createQueryBuilder('tpl')
36
            ->where('tpl.name = :name')
37
            ->setParameter('name', $name)
38
            ->addSelect(
39
                'CASE WHEN tpl.locale = :preferredLocale THEN 2
40
                      WHEN tpl.locale = :fallbackLocale THEN 1
41
                      ELSE 0
42
                 END AS HIDDEN localePreference'
43
            )
44
            ->setParameter('preferredLocale', $preferredLocale)
45
            ->setParameter('fallbackLocale', $fallbackLocale)
46
            ->orderBy('localePreference', 'DESC')
47
            ->setMaxResults(1)
48
            ->getQuery()
49
            ->getOneOrNullResult();
50
    }
51
52
    /**
53
     * Removes all email templates.
54
     *
55
     * We hydrate all templates and remove them through the entitymanager so that they get
56
     * removed from the IdentityMap. This to prevent issues when replaying the events, where
57
     * deleting them with a delete query would cause errors due to templates not being found.
58
     */
59
    public function removeAll()
60
    {
61
        $templates = $this->findAll();
62
        $em = $this->getEntityManager();
63
64
        foreach ($templates as $template) {
65
            $em->remove($template);
66
        }
67
68
        $em->flush();
69
70
        unset($templates);
71
    }
72
73
    public function save(EmailTemplate $template)
74
    {
75
        $entityManager = $this->getEntityManager();
76
        $entityManager->persist($template);
77
        $entityManager->flush();
78
    }
79
}
80