Completed
Push — feature/fix-ra-listing-endpoin... ( 8ce24d )
by
unknown
02:42
created

findByIdentityIdAndRaInstitutionWithContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
class RaListingRepository extends EntityRepository
34
{
35
    /**
36
     * @var InstitutionAuthorizationRepositoryFilter
37
     */
38
    private $authorizationRepositoryFilter;
39
40
    public function __construct(
41
        EntityManager $em,
42
        Mapping\ClassMetadata $class,
43
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
44
    ) {
45
        parent::__construct($em, $class);
46
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
47
    }
48
49
    /**
50
     * @param IdentityId $identityId The RA's identity id.
51
     * @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...
52
     */
53
    public function findByIdentityId(IdentityId $identityId)
54
    {
55
        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...
56
    }
57
58
    /**
59
     * @param IdentityId $identityId The RA's identity id.
60
     * @param Institution $raInstitution
61
     * @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...
62
     */
63
    public function findByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
64
    {
65
        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...
66
            'identityId' => (string) $identityId,
67
            'raInstitution' => (string) $raInstitution,
68
        ]);
69
    }
70
71
72
    /**
73
     * @param IdentityId $identityId The RA's identity id.
74
     * @param Institution $raInstitution
75
     * @param InstitutionAuthorizationContextInterface $authorizationContext
76
     * @return null|RaListing
77
     */
78
    public function findByIdentityIdAndRaInstitutionWithContext(IdentityId $identityId, Institution $raInstitution, InstitutionAuthorizationContextInterface $authorizationContext)
79
    {
80
        $queryBuilder = $this->createQueryBuilder('r')
81
            ->where('r.identityId = :identityId')
82
            ->andWhere('r.raInstitution = :raInstitution')
83
            ->setParameter('identityId', $identityId)
84
            ->setParameter('raInstitution', (string)$raInstitution)
85
            ->orderBy('r.raInstitution');
86
87
        // Modify query to filter on authorization
88
        $this->authorizationRepositoryFilter->filter(
89
            $queryBuilder,
90
            $authorizationContext,
91
            'r.raInstitution',
92
            'iac'
93
        );
94
95
        return $queryBuilder->getQuery()->getOneOrNullResult();
96
    }
97
98
    /**
99
     * @param IdentityId $identityId The RA's identity id.
100
     * @param Institution $institution
101
     * @return RaListing[]
102
     */
103
    public function findByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
104
    {
105
        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...
106
            'identityId' => (string) $identityId,
107
            'institution' => (string) $institution,
108
        ]);
109
    }
110
111
    public function save(RaListing $raListingEntry)
112
    {
113
        $this->getEntityManager()->persist($raListingEntry);
114
        $this->getEntityManager()->flush();
115
    }
116
117
    /**
118
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
119
     * @SuppressWarnings(PHPMD.NPathComplexity)      below complex or hard to maintain.
120
     *
121
     * @param RaListingQuery $query
122
     * @return \Doctrine\ORM\Query
123
     */
124
    public function createSearchQuery(RaListingQuery $query)
125
    {
126
        $queryBuilder = $this->createQueryBuilder('r');
127
128
        // Modify query to filter on authorization
129
        $this->authorizationRepositoryFilter->filter(
130
            $queryBuilder,
131
            $query->authorizationContext,
132
            'r.raInstitution',
133
            'iac'
134
        );
135
136
        if ($query->institution) {
137
            $queryBuilder
138
                ->andWhere('r.institution = :institution')
139
                ->setParameter('institution', $query->institution);
140
        }
141
142
        if ($query->identityId) {
143
            $queryBuilder
144
                ->andWhere('r.identityId = :identityId')
145
                ->setParameter('identityId', (string) $query->identityId);
146
        }
147
148
        if ($query->name) {
149
            $queryBuilder
150
                ->andWhere('r.commonName LIKE :name')
151
                ->setParameter('name', sprintf('%%%s%%', $query->name));
152
        }
153
154
        if ($query->email) {
155
            $queryBuilder
156
                ->andWhere('r.email LIKE :email')
157
                ->setParameter('email', sprintf('%%%s%%', $query->email));
158
        }
159
160
        if ($query->role) {
161
            $queryBuilder
162
                ->andWhere('r.role = :role')
163
                ->setParameter('role', (string) $query->role);
164
        }
165
166
        if ($query->raInstitution) {
167
            $queryBuilder
168
                ->andWhere('r.raInstitution = :raInstitution')
169
                ->setParameter('raInstitution', (string) $query->raInstitution);
170
        }
171
172
        if (!$query->orderBy) {
173
            return $queryBuilder->getQuery();
174
        }
175
176
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
177
178
        switch ($query->orderBy) {
179
            case 'commonName':
180
                $queryBuilder->orderBy('r.commonName', $orderDirection);
181
                break;
182
            default:
183
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
184
        }
185
186
        return $queryBuilder->getQuery();
187
    }
188
189
    /**
190
     * @param Institution $raInstitution
191
     * @return ArrayCollection
192
     */
193
    public function listRasFor(Institution $raInstitution)
194
    {
195
        $listings = $this->createQueryBuilder('rl')
196
            ->where('rl.raInstitution = :institution')
197
            ->setParameter('institution', $raInstitution)
198
            ->getQuery()
199
            ->getResult();
200
201
        return new ArrayCollection($listings);
202
    }
203
204
    /**
205
     * @param IdentityId $identityId
206
     * @return void
207
     */
208 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...
209
    {
210
        $this->getEntityManager()->createQueryBuilder()
211
            ->delete($this->_entityName, 'ral')
212
            ->where('ral.identityId = :identityId')
213
            ->andWhere('ral.raInstitution = :institution')
214
            ->setParameter('identityId', $identityId->getIdentityId())
215
            ->getQuery()
216
            ->execute();
217
    }
218
219
    /**
220
     * @param IdentityId $identityId
221
     * @param Institution $raInstitution
222
     * @return void
223
     */
224
    public function removeByIdentityIdAndRaInstitution(IdentityId $identityId, Institution $raInstitution)
225
    {
226
        $this->getEntityManager()->createQueryBuilder()
227
            ->delete($this->_entityName, 'ral')
228
            ->where('ral.identityId = :identityId')
229
            ->andWhere('ral.raInstitution = :institution')
230
            ->setParameter('identityId', $identityId->getIdentityId())
231
            ->setParameter('institution', $raInstitution)
232
            ->getQuery()
233
            ->execute();
234
    }
235
236
    /**
237
     * @param IdentityId $identityId
238
     * @param Institution $institution
239
     * @return void
240
     */
241
    public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $institution)
242
    {
243
        $this->getEntityManager()->createQueryBuilder()
244
            ->delete($this->_entityName, 'ral')
245
            ->where('ral.identityId = :identityId')
246
            ->andWhere('ral.institution = :institution')
247
            ->setParameter('identityId', $identityId->getIdentityId())
248
            ->setParameter('institution', $institution)
249
            ->getQuery()
250
            ->execute();
251
    }
252
}
253