Completed
Push — feature/profile-retrieve-impli... ( 3a9ede...231202 )
by
unknown
03:23
created

RaListingRepository::getExplicitInstitutionsFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\Common\Collections\ArrayCollection;
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\EntityRepository;
24
use Doctrine\ORM\Mapping;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
29
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaListingQuery;
32
33
/**
34
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
35
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
 */
37
class RaListingRepository extends EntityRepository
38
{
39
    /**
40
     * @var InstitutionAuthorizationRepositoryFilter
41
     */
42
    private $authorizationRepositoryFilter;
43
44
    public function __construct(
45
        EntityManager $em,
46
        Mapping\ClassMetadata $class,
47
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
48
    ) {
49
        parent::__construct($em, $class);
50
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
51
    }
52
53
    /**
54
     * @param IdentityId $identityId The RA's identity id.
55
     * @return null|RaListing[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
     */
57
    public function findByIdentityId(IdentityId $identityId)
58
    {
59
        return parent::findBy(['identityId' => (string) $identityId]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findBy() instead of findByIdentityId()). Are you sure this is correct? If so, you might want to change this to $this->findBy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
60
    }
61
62
    /**
63
     * @param IdentityId $identityId The RA's identity id.
64
     * @param Institution $raInstitution
65
     * @return null|RaListing
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67
    public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
68
    {
69
        return parent::findOneBy([
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findOneBy() instead of findByIdentityIdAndRaInstitution()). Are you sure this is correct? If so, you might want to change this to $this->findOneBy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
70
            'identityId' => (string) $identityId,
71
            'raInstitution' => (string) $raInstitution,
72
        ]);
73
    }
74
75
76
    /**
77
     * @param IdentityId $identityId The RA's identity id.
78
     * @param Institution $raInstitution
79
     * @param InstitutionAuthorizationContextInterface $authorizationContext
80
     * @return null|RaListing
81
     */
82
    public function findByIdentityIdAndRaInstitutionWithContext(
83
        IdentityId $identityId,
84
        Institution $raInstitution,
85
        InstitutionAuthorizationContextInterface $authorizationContext
86
    ) {
87
        $queryBuilder = $this->createQueryBuilder('r')
88
            ->where('r.identityId = :identityId')
89
            ->andWhere('r.raInstitution = :raInstitution')
90
            ->setParameter('identityId', $identityId)
91
            ->setParameter('raInstitution', (string)$raInstitution)
92
            ->orderBy('r.raInstitution');
93
94
        // Modify query to filter on authorization
95
        $this->authorizationRepositoryFilter->filter(
96
            $queryBuilder,
97
            $authorizationContext,
98
            'r.raInstitution',
99
            'iac'
100
        );
101
102
        return $queryBuilder->getQuery()->getOneOrNullResult();
103
    }
104
105
    /**
106
     * @param IdentityId $identityId The RA's identity id.
107
     * @param Institution $institution
108
     * @return RaListing[]
109
     */
110
    public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
111
    {
112
        return parent::findBy([
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findBy() instead of findByIdentityIdAndInstitution()). Are you sure this is correct? If so, you might want to change this to $this->findBy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
113
            'identityId' => (string) $identityId,
114
            'institution' => (string) $institution,
115
        ]);
116
    }
117
118
    public function save(RaListing $raListingEntry)
119
    {
120
        $this->getEntityManager()->persist($raListingEntry);
121
        $this->getEntityManager()->flush();
122
    }
123
124
    /**
125
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
126
     * @SuppressWarnings(PHPMD.NPathComplexity)      below complex or hard to maintain.
127
     *
128
     * @param RaListingQuery $query
129
     * @return \Doctrine\ORM\Query
130
     */
131
    public function createSearchQuery(RaListingQuery $query)
132
    {
133
        $queryBuilder = $this->createQueryBuilder('r');
134
135
        // Modify query to filter on authorization
136
        $this->authorizationRepositoryFilter->filter(
137
            $queryBuilder,
138
            $query->authorizationContext,
139
            'r.raInstitution',
140
            'iac'
141
        );
142
143
        if ($query->institution) {
144
            $queryBuilder
145
                ->andWhere('r.institution = :institution')
146
                ->setParameter('institution', $query->institution);
147
        }
148
149
        if ($query->identityId) {
150
            $queryBuilder
151
                ->andWhere('r.identityId = :identityId')
152
                ->setParameter('identityId', (string) $query->identityId);
153
        }
154
155
        if ($query->name) {
156
            $queryBuilder
157
                ->andWhere('r.commonName LIKE :name')
158
                ->setParameter('name', sprintf('%%%s%%', $query->name));
159
        }
160
161
        if ($query->email) {
162
            $queryBuilder
163
                ->andWhere('r.email LIKE :email')
164
                ->setParameter('email', sprintf('%%%s%%', $query->email));
165
        }
166
167
        if ($query->role) {
168
            $queryBuilder
169
                ->andWhere('r.role = :role')
170
                ->setParameter('role', (string) $query->role);
171
        }
172
173
        if ($query->raInstitution) {
174
            $queryBuilder
175
                ->andWhere('r.raInstitution = :raInstitution')
176
                ->setParameter('raInstitution', (string) $query->raInstitution);
177
        }
178
179
        if (!$query->orderBy) {
180
            return $queryBuilder->getQuery();
181
        }
182
183
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
184
185
        switch ($query->orderBy) {
186
            case 'commonName':
187
                $queryBuilder->orderBy('r.commonName', $orderDirection);
188
                break;
189
            default:
190
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
191
        }
192
193
        return $queryBuilder->getQuery();
194
    }
195
196
    /**
197
     * @param Institution $raInstitution
198
     * @return ArrayCollection
199
     */
200
    public function listRasFor(Institution $raInstitution)
201
    {
202
        $listings = $this->createQueryBuilder('rl')
203
            ->where('rl.raInstitution = :institution')
204
            ->setParameter('institution', $raInstitution)
205
            ->getQuery()
206
            ->getResult();
207
208
        return new ArrayCollection($listings);
209
    }
210
211
    /**
212
     * @param IdentityId $identityId
213
     * @return void
214
     */
215 View Code Duplication
    public function removeByIdentityId(IdentityId $identityId)
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...
216
    {
217
        $this->getEntityManager()->createQueryBuilder()
218
            ->delete($this->_entityName, 'ral')
219
            ->where('ral.identityId = :identityId')
220
            ->andWhere('ral.raInstitution = :institution')
221
            ->setParameter('identityId', $identityId->getIdentityId())
222
            ->getQuery()
223
            ->execute();
224
    }
225
226
    /**
227
     * @param IdentityId $identityId
228
     * @param Institution $raInstitution
229
     * @return void
230
     */
231 View Code Duplication
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
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...
232
    {
233
        $this->getEntityManager()->createQueryBuilder()
234
            ->delete($this->_entityName, 'ral')
235
            ->where('ral.identityId = :identityId')
236
            ->andWhere('ral.raInstitution = :institution')
237
            ->setParameter('identityId', $identityId->getIdentityId())
238
            ->setParameter('institution', $raInstitution)
239
            ->getQuery()
240
            ->execute();
241
    }
242
243
    /**
244
     * @param IdentityId $identityId
245
     * @param Institution $institution
246
     * @return void
247
     */
248 View Code Duplication
    public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
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...
249
    {
250
        $this->getEntityManager()->createQueryBuilder()
251
            ->delete($this->_entityName, 'ral')
252
            ->where('ral.identityId = :identityId')
253
            ->andWhere('ral.institution = :institution')
254
            ->setParameter('identityId', $identityId->getIdentityId())
255
            ->setParameter('institution', $institution)
256
            ->getQuery()
257
            ->execute();
258
    }
259
}
260