Completed
Pull Request — develop (#194)
by Nic
08:12 queued 03:46
created

RaCandidateRepository::createSearchQuery()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 18

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 8
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\ApiBundle\Identity\Repository;
20
21
use Doctrine\ORM\EntityRepository;
22
use Surfnet\Stepup\Identity\Value\IdentityId;
23
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery;
26
27
class RaCandidateRepository extends EntityRepository
28
{
29
    /**
30
     * @param RaCandidate $raCandidate
31
     * @return void
32
     */
33
    public function save(RaCandidate $raCandidate)
34
    {
35
        $this->getEntityManager()->persist($raCandidate);
36
        $this->getEntityManager()->flush();
37
    }
38
39
    /**
40
     * @param IdentityId $identityId
41
     * @return void
42
     */
43
    public function removeByIdentityId(IdentityId $identityId)
44
    {
45
        $raCandidate = $this->findByIdentityId($identityId);
46
47
        if (!$raCandidate) {
48
            return;
49
        }
50
51
        $this->getEntityManager()->remove($raCandidate);
52
        $this->getEntityManager()->flush();
53
    }
54
55
    /**
56
     * @param string[] $nameIds
57
     * @return void
58
     */
59
    public function removeByNameIds($nameIds)
60
    {
61
        $raCandidates = $this->findByNameIds($nameIds);
62
63
        $em = $this->getEntityManager();
64
        foreach ($raCandidates as $raCandidate) {
65
            $em->remove($raCandidate);
66
        }
67
68
        $em->flush();
69
    }
70
71
    /**
72
     * @param RaCandidateQuery $query
73
     * @return \Doctrine\ORM\Query
74
     */
75 View Code Duplication
    public function createSearchQuery(RaCandidateQuery $query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $queryBuilder = $this->createQueryBuilder('rac')
78
            ->where('rac.institution = :institution')
79
            ->setParameter('institution', $query->institution);
80
81
        if ($query->commonName) {
82
            $queryBuilder
83
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
84
                ->setParameter('commonName', $query->commonName);
85
        }
86
87
        if ($query->email) {
88
            $queryBuilder
89
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
90
                ->setParameter('email', $query->email);
91
        }
92
93
        if (!empty($query->secondFactorTypes)) {
94
            $queryBuilder
95
                ->innerJoin(VettedSecondFactor::class, 'vsf')
96
                ->andWhere('vsf.type IN (:secondFactorTypes)')
97
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
98
        }
99
100
        return $queryBuilder->getQuery();
101
    }
102
103
    /**
104
     * @param string[] $sraaList
105
     * @return RaCandidate[]
106
     */
107
    public function findByNameIds(array $sraaList)
108
    {
109
        return $this->createQueryBuilder('rac')
110
            ->where('rac.nameId IN (:sraaList)')
111
            ->setParameter('sraaList', $sraaList)
112
            ->getQuery()
113
            ->getResult();
114
    }
115
116
    /**
117
     * @param string $identityId
118
     * @return null|RaCandidate
119
     * @throws \Doctrine\ORM\NonUniqueResultException
120
     */
121
    public function findByIdentityId($identityId)
122
    {
123
        return $this->createQueryBuilder('rac')
124
            ->where('rac.identityId = :identityId')
125
            ->setParameter('identityId', $identityId)
126
            ->getQuery()
127
            ->getOneOrNullResult();
128
    }
129
}
130