EmailTemplateRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneByName() 0 18 1
A removeAll() 0 12 2
A __construct() 0 3 1
A save() 0 5 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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\ManagementBundle\Configuration\Repository;
20
21
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Doctrine\Persistence\ManagerRegistry;
23
use Surfnet\StepupMiddleware\ManagementBundle\Configuration\Entity\EmailTemplate;
24
25
/**
26
 * @extends ServiceEntityRepository<EmailTemplate>
27
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
28
final class EmailTemplateRepository extends ServiceEntityRepository
29
{
30
    public function __construct(ManagerRegistry $registry)
31
    {
32
        parent::__construct($registry, EmailTemplate::class);
33
    }
34
35
    /**
36
     * @param string $name
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
37
     * @param string $preferredLocale
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
38
     * @param string $fallbackLocale
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
39
     * @return EmailTemplate|null
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
40
     */
41
    public function findOneByName(mixed $name, mixed $preferredLocale, mixed $fallbackLocale): ?EmailTemplate
42
    {
43
        return $this
44
            ->createQueryBuilder('tpl')
45
            ->where('tpl.name = :name')
46
            ->setParameter('name', $name)
47
            ->addSelect(
48
                'CASE WHEN tpl.locale = :preferredLocale THEN 2
49
                      WHEN tpl.locale = :fallbackLocale THEN 1
50
                      ELSE 0
51
                 END AS HIDDEN localePreference',
52
            )
53
            ->setParameter('preferredLocale', $preferredLocale)
54
            ->setParameter('fallbackLocale', $fallbackLocale)
55
            ->orderBy('localePreference', 'DESC')
56
            ->setMaxResults(1)
57
            ->getQuery()
58
            ->getOneOrNullResult();
59
    }
60
61
    /**
62
     * Removes all email templates.
63
     *
64
     * We hydrate all templates and remove them through the entitymanager so that they get
65
     * removed from the IdentityMap. This to prevent issues when replaying the events, where
66
     * deleting them with a delete query would cause errors due to templates not being found.
67
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
68
    public function removeAll(): void
69
    {
70
        $templates = $this->findAll();
71
        $em = $this->getEntityManager();
72
73
        foreach ($templates as $template) {
74
            $em->remove($template);
75
        }
76
77
        $em->flush();
78
79
        unset($templates);
80
    }
81
82
    public function save(EmailTemplate $template): void
83
    {
84
        $entityManager = $this->getEntityManager();
85
        $entityManager->persist($template);
86
        $entityManager->flush();
87
    }
88
}
89