Completed
Pull Request — develop (#301)
by
unknown
05:22 queued 02:27
created

RaCandidateRepository::createSearchQuery()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 32
nop 1
dl 0
loc 66
rs 8.1195
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\EntityManager;
22
use Doctrine\ORM\EntityRepository;
23
use Doctrine\ORM\Mapping;
24
use Doctrine\ORM\Query\Expr\Join;
25
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
26
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
27
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\InstitutionAuthorization;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VettedSecondFactor;
32
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaCandidateQuery;
33
34
/**
35
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
37
 */
38
class RaCandidateRepository extends EntityRepository
39
{
40
    /**
41
     * @var InstitutionAuthorizationRepositoryFilter
42
     */
43
    private $authorizationRepositoryFilter;
44
45
    public function __construct(
46
        EntityManager $em,
47
        Mapping\ClassMetadata $class,
48
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
49
    ) {
50
        parent::__construct($em, $class);
51
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
52
    }
53
54
    /**
55
     * @param RaCandidateQuery $query
56
     * @return \Doctrine\ORM\Query
57
     */
58
    public function createSearchQuery(RaCandidateQuery $query)
59
    {
60
        // Base query to get all allowed ra candidates
61
        $queryBuilder = $this->getEntityManager()->createQueryBuilder()
62
            ->select('i.id as identity_id, i.institution, i.commonName as common_name, i.email, i.nameId AS name_id, a.institution AS ra_institution')
63
            ->from(VettedSecondFactor::class, 'vsf')
64
            ->innerJoin(Identity::class, 'i', Join::WITH, "vsf.identityId = i.id")
65
            ->innerJoin(InstitutionAuthorization::class, 'a', Join::WITH,
66
                "a.institutionRole = 'select_raa' AND a.institutionRelation = i.institution");
67
68
        // Filter out candidates who are already ra
69
        // Todo: filter out SRAA's ?
70
        $subQuery = $this->getEntityManager()->createQueryBuilder()
71
            ->select('l')
72
            ->from(RaListing::class, "l")
73
            ->where("l.identityId = i.id AND l.raInstitution = a.institution");
74
75
        $queryBuilder->andWhere($queryBuilder->expr()->not($queryBuilder->expr()->exists($subQuery->getDQL())));
76
77
78
        // Modify query to filter on authorization:
79
        // For the RA candidates we want the identities that we could make RA. Because we then need to look at the
80
        // select_raa's we have to look at the institution of the candidate because that's the institution we could
81
        // select RA's from. Hence the 'rac.institution'.
82
        $this->authorizationRepositoryFilter->filter(
83
            $queryBuilder,
84
            $query->authorizationContext,
85
            'i.institution',
86
            'iac'
87
        );
88
89
        if ($query->institution) {
90
            $queryBuilder
91
                ->andWhere('i.institution = :institution')
92
                ->setParameter('institution', $query->institution);
93
        }
94
95
        if ($query->commonName) {
96
            $queryBuilder
97
                ->andWhere('i.commonName LIKE :commonName')
98
                ->setParameter('commonName', sprintf('%%%s%%', $query->commonName));
99
        }
100
101
        if ($query->email) {
102
            $queryBuilder
103
                ->andWhere('i.email LIKE :email')
104
                ->setParameter('email', sprintf('%%%s%%', $query->email));
105
        }
106
107
        if (!empty($query->secondFactorTypes)) {
108
            $queryBuilder
109
                //->innerJoin(VettedSecondFactor::class, 'vsf', Join::WITH, 'rac.identityId = vsf.identityId')
110
                ->andWhere('vsf.type IN (:secondFactorTypes)')
111
                ->setParameter('secondFactorTypes', $query->secondFactorTypes);
112
        }
113
114
        if (!empty($query->raInstitution)) {
115
            $queryBuilder
116
                ->andWhere('a.raInstitution = :raInstitution')
117
                ->setParameter('raInstitution', $query->raInstitution);
118
        }
119
120
        $queryBuilder->groupBy('i.id');
121
122
        return $queryBuilder->getQuery();
123
    }
124
125
    /**
126
     * @param RaCandidateQuery $query
127
     * @return \Doctrine\ORM\Query
128
     */
129
    public function createOptionsQuery(RaCandidateQuery $query)
130
    {
131
        $queryBuilder = $this->getEntityManager()->createQueryBuilder()
132
            ->select('a.institution')
133
            ->from(InstitutionAuthorization::class, 'a')
134
            ->where("a.institutionRole = 'select_raa'");
135
136
137
        // Modify query to filter on authorization:
138
        // For the RA candidates we want the identities that we could make RA. Because we then need to look at the
139
        // select_raa's we have to look at the institution of the candidate because that's the institution we could
140
        // select RA's from. Hence the 'rac.institution'.
141
        $this->authorizationRepositoryFilter->filter(
142
            $queryBuilder,
143
            $query->authorizationContext,
144
            'a.institution',
145
            'iac'
146
        );
147
148
        return $queryBuilder->getQuery();
149
    }
150
151
    /**
152
     * @param string $identityId
153
     * @param InstitutionAuthorizationContextInterface $authorizationContext
154
     * @return RaCandidate[]
155
     */
156
    public function findAllRaasByIdentityId($identityId, InstitutionAuthorizationContextInterface $authorizationContext)
157
    {
158
        $queryBuilder = $this->createQueryBuilder('rac')
159
            ->where('rac.identityId = :identityId')
160
            ->setParameter('identityId', $identityId)
161
            ->orderBy('rac.raInstitution');
162
163
        // Modify query to filter on authorization:
164
        // For the RA candidates we want the identities that we could make RA. Because we then need to look at the
165
        // select_raa's we have to look at the institution of the candidate because that's the institution we could
166
        // select RA's from. Hence the 'rac.institution'.
167
        $this->authorizationRepositoryFilter->filter(
168
            $queryBuilder,
169
            $authorizationContext,
170
            'rac.institution',
171
            'iac'
172
        );
173
174
        return $queryBuilder->getQuery()->getResult();
175
    }
176
}
177