Completed
Push — master ( a3c457...aab356 )
by
unknown
03:38
created

RaCandidateRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A removeByIdentityId() 0 11 2
A removeByNameIds() 0 11 2
B createSearchQuery() 0 27 4
A findByNameIds() 0 8 1
A findByIdentityId() 0 8 1
A merge() 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
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Repository;
20
21
use Doctrine\ORM\EntityRepository;
22
use Doctrine\ORM\Query\Expr\Join;
23
use Surfnet\Stepup\Identity\Value\IdentityId;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery;
27
28
class RaCandidateRepository extends EntityRepository
29
{
30
    /**
31
     * @param RaCandidate $raCandidate
32
     * @return void
33
     */
34
    public function merge(RaCandidate $raCandidate)
35
    {
36
        $this->getEntityManager()->merge($raCandidate);
37
        $this->getEntityManager()->flush();
38
    }
39
40
    /**
41
     * @param IdentityId $identityId
42
     * @return void
43
     */
44
    public function removeByIdentityId(IdentityId $identityId)
45
    {
46
        $raCandidate = $this->findByIdentityId($identityId);
47
48
        if (!$raCandidate) {
49
            return;
50
        }
51
52
        $this->getEntityManager()->remove($raCandidate);
53
        $this->getEntityManager()->flush();
54
    }
55
56
    /**
57
     * @param string[] $nameIds
58
     * @return void
59
     */
60
    public function removeByNameIds($nameIds)
61
    {
62
        $raCandidates = $this->findByNameIds($nameIds);
63
64
        $em = $this->getEntityManager();
65
        foreach ($raCandidates as $raCandidate) {
66
            $em->remove($raCandidate);
67
        }
68
69
        $em->flush();
70
    }
71
72
    /**
73
     * @param RaCandidateQuery $query
74
     * @return \Doctrine\ORM\Query
75
     */
76
    public function createSearchQuery(RaCandidateQuery $query)
77
    {
78
        $queryBuilder = $this->createQueryBuilder('rac')
79
            ->where('rac.institution = :institution')
80
            ->setParameter('institution', $query->institution);
81
82
        if ($query->commonName) {
83
            $queryBuilder
84
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
85
                ->setParameter('commonName', $query->commonName);
86
        }
87
88
        if ($query->email) {
89
            $queryBuilder
90
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
91
                ->setParameter('email', $query->email);
92
        }
93
94
        if (!empty($query->secondFactorTypes)) {
95
            $queryBuilder
96
                ->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
97
                ->andWhere('vsf.type IN (:secondFactorTypes)')
98
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
99
        }
100
101
        return $queryBuilder->getQuery();
102
    }
103
104
    /**
105
     * @param string[] $sraaList
106
     * @return RaCandidate[]
107
     */
108
    public function findByNameIds(array $sraaList)
109
    {
110
        return $this->createQueryBuilder('rac')
111
            ->where('rac.nameId IN (:sraaList)')
112
            ->setParameter('sraaList', $sraaList)
113
            ->getQuery()
114
            ->getResult();
115
    }
116
117
    /**
118
     * @param string $identityId
119
     * @return null|RaCandidate
120
     * @throws \Doctrine\ORM\NonUniqueResultException
121
     */
122
    public function findByIdentityId($identityId)
123
    {
124
        return $this->createQueryBuilder('rac')
125
            ->where('rac.identityId = :identityId')
126
            ->setParameter('identityId', $identityId)
127
            ->getQuery()
128
            ->getOneOrNullResult();
129
    }
130
}
131