Completed
Push — feature/provide-ra-listing-sea... ( 25358b )
by Michiel
03:06
created

findByIdentityIdAndRaInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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