Completed
Push — develop ( 63a64b...1c0683 )
by
unknown
06:54 queued 04:03
created

removeByIdentityIdAndInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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