Completed
Pull Request — develop (#124)
by Boy
23:30 queued 18:57
created

RaCandidateRepository::createSearchQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 13
nc 4
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\Query\RaCandidateQuery;
25
26
class RaCandidateRepository extends EntityRepository
27
{
28
    /**
29
     * @param RaCandidate $raCandidate
30
     * @return void
31
     */
32
    public function save(RaCandidate $raCandidate)
33
    {
34
        $this->getEntityManager()->persist($raCandidate);
35
        $this->getEntityManager()->flush();
36
    }
37
38
    /**
39
     * @param IdentityId $identityId
40
     * @return void
41
     */
42
    public function removeByIdentityId(IdentityId $identityId)
43
    {
44
        $raCandidate = $this->findByIdentityId($identityId);
45
46
        if (!$raCandidate) {
47
            return;
48
        }
49
50
        $this->getEntityManager()->remove($raCandidate);
51
        $this->getEntityManager()->flush();
52
    }
53
54
    /**
55
     * @param string[] $sraaList
56
     * @return void
57
     */
58
    public function removeByNameIds($sraaList)
59
    {
60
        $raCandidates = $this->findByNameIds($sraaList);
61
62
        $em = $this->getEntityManager();
63
        foreach ($raCandidates as $raCandidate) {
64
            $em->remove($raCandidate);
65
        }
66
67
        $em->flush();
68
    }
69
70
    /**
71
     * @param RaCandidateQuery $query
72
     * @return \Doctrine\ORM\Query
73
     */
74
    public function createSearchQuery(RaCandidateQuery $query)
75
    {
76
        $queryBuilder = $this->createQueryBuilder('rac')
77
            ->where('rac.institution = :institution')
78
            ->setParameter('institution', $query->institution);
79
80
        if ($query->commonName) {
81
            $queryBuilder
82
                ->andWhere('MATCH_AGAINST(rac.commonName, :commonName) > 0')
83
                ->setParameter('commonName', $query->commonName);
84
        }
85
86
        if ($query->email) {
87
            $queryBuilder
88
                ->andWhere('MATCH_AGAINST(rac.email, :email) > 0')
89
                ->setParameter('email', $query->email);
90
        }
91
92
        return $queryBuilder->getQuery();
93
    }
94
95
    /**
96
     * @param string[] $sraaList
97
     * @return RaCandidate[]
98
     */
99
    public function findByNameIds(array $sraaList)
100
    {
101
        return $this->createQueryBuilder('rac')
102
            ->where('rac.nameId IN (:sraaList)')
103
            ->setParameter('sraaList', $sraaList)
104
            ->getQuery()
105
            ->getResult();
106
    }
107
108
    /**
109
     * @param string $identityId
110
     * @return null|RaCandidate
111
     * @throws \Doctrine\ORM\NonUniqueResultException
112
     */
113
    public function findByIdentityId($identityId)
114
    {
115
        return $this->createQueryBuilder('rac')
116
            ->where('rac.identityId = :identityId')
117
            ->setParameter('identityId', $identityId)
118
            ->getQuery()
119
            ->getOneOrNullResult();
120
    }
121
}
122