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

EmailTemplateRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findOneByName() 0 19 1
A removeAll() 0 13 2
A save() 0 6 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