Completed
Push — feature/fine-grained-authoriza... ( 479a18...68ddb3 )
by
unknown
133:25 queued 130:10
created

RaListingRepository::createSearchQuery()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.6417
c 0
b 0
f 0
cc 6
nc 20
nop 1
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 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...
51
     */
52
    public function findByIdentityId(IdentityId $identityId)
53
    {
54
        return parent::findOneBy(['identityId' => (string) $identityId]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findOneBy() instead of findByIdentityId()). 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...
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.identityId', 'r.institution', 'r.raInstitution'],
102
            'r.institution',
103
            'iac'
104
        );
105
106
        if ($query->institution) {
107
            $queryBuilder
108
                ->andWhere('r.institution = :institution')
109
                ->setParameter('institution', $query->institution);
110
        }
111
112
        if ($query->identityId) {
113
            $queryBuilder
114
                ->andWhere('r.identityId = :identityId')
115
                ->setParameter('identityId', (string) $query->identityId);
116
        }
117
118
        if (!$query->orderBy) {
119
            return $queryBuilder->getQuery();
120
        }
121
122
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
123
124
        switch ($query->orderBy) {
125
            case 'commonName':
126
                $queryBuilder->orderBy('r.commonName', $orderDirection);
127
                break;
128
            default:
129
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
130
        }
131
132
        return $queryBuilder->getQuery();
133
    }
134
135
    /**
136
     * @param Institution $raInstitution
137
     * @return ArrayCollection
138
     */
139
    public function listRasFor(Institution $raInstitution)
140
    {
141
        $listings = $this->createQueryBuilder('rl')
142
            ->where('rl.raInstitution = :institution')
143
            ->setParameter('institution', $raInstitution)
144
            ->getQuery()
145
            ->getResult();
146
147
        return new ArrayCollection($listings);
148
    }
149
150
    /**
151
     * @param IdentityId $identityId
152
     * @return void
153
     */
154 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...
155
    {
156
        $this->getEntityManager()->createQueryBuilder()
157
            ->delete($this->_entityName, 'ral')
158
            ->where('ral.identityId = :identityId')
159
            ->andWhere('ral.raInstitution = :institution')
160
            ->setParameter('identityId', $identityId->getIdentityId())
161
            ->getQuery()
162
            ->execute();
163
    }
164
165
    /**
166
     * @param IdentityId $identityId
167
     * @param Institution $raInstitution
168
     * @return void
169
     */
170 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...
171
    {
172
        $this->getEntityManager()->createQueryBuilder()
173
            ->delete($this->_entityName, 'ral')
174
            ->where('ral.identityId = :identityId')
175
            ->andWhere('ral.raInstitution = :institution')
176
            ->setParameter('identityId', $identityId->getIdentityId())
177
            ->setParameter('institution', $raInstitution)
178
            ->getQuery()
179
            ->execute();
180
    }
181
182
    /**
183
     * @param IdentityId $identityId
184
     * @param Institution $institution
185
     * @return void
186
     */
187 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...
188
    {
189
        $this->getEntityManager()->createQueryBuilder()
190
            ->delete($this->_entityName, 'ral')
191
            ->where('ral.identityId = :identityId')
192
            ->andWhere('ral.institution = :institution')
193
            ->setParameter('identityId', $identityId->getIdentityId())
194
            ->setParameter('institution', $institution)
195
            ->getQuery()
196
            ->execute();
197
    }
198
}
199