Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
40:56 queued 21:52
created

RaListingRepository::getRaasByInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
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 findByIdentityIdAndInstitution(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 findByIdentityIdAndInstitution()). 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
    public function save(RaListing $raListingEntry)
71
    {
72
        $this->getEntityManager()->persist($raListingEntry);
73
        $this->getEntityManager()->flush();
74
    }
75
76
    /**
77
     * @param RaListingQuery $query
78
     * @return \Doctrine\ORM\Query
79
     */
80
    public function createSearchQuery(RaListingQuery $query)
81
    {
82
        $queryBuilder = $this->createQueryBuilder('r');
83
84
        // Modify query to filter on authorization
85
        $this->authorizationRepositoryFilter->filter(
86
            $queryBuilder,
87
            $query->authorizationContext,
88
            ['r.identityId', 'r.institution'],
89
            'r.institution',
90
            'iac'
91
        );
92
93
        if ($query->institution) {
94
            $queryBuilder
95
                ->andWhere('r.institution = :institution')
96
                ->setParameter('institution', $query->institution);
97
        }
98
99
        if (!$query->orderBy) {
100
            return $queryBuilder->getQuery();
101
        }
102
103
        $orderDirection = $query->orderDirection === 'asc' ? 'ASC' : 'DESC';
104
105
        switch ($query->orderBy) {
106
            case 'commonName':
107
                $queryBuilder->orderBy('r.commonName', $orderDirection);
108
                break;
109
            default:
110
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
111
        }
112
113
        return $queryBuilder->getQuery();
114
    }
115
116
    /**
117
     * @param Institution $raInstitution
118
     * @return ArrayCollection
119
     */
120
    public function listRasFor(Institution $raInstitution)
121
    {
122
        $listings = $this->createQueryBuilder('rl')
123
            ->where('rl.ra_institution = :institution')
124
            ->setParameter('institution', $raInstitution)
125
            ->getQuery()
126
            ->getResult();
127
128
        return new ArrayCollection($listings);
129
    }
130
131
    /**
132
     * @param IdentityId $identityId
133
     * @return void
134
     */
135 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...
136
    {
137
        $this->getEntityManager()->createQueryBuilder()
138
            ->delete($this->_entityName, 'ral')
139
            ->where('ral.identityId = :identityId')
140
            ->andWhere('rl.ra_institution = :institution')
141
            ->setParameter('identityId', $identityId->getIdentityId())
142
            ->getQuery()
143
            ->execute();
144
    }
145
146
    /**
147
     * @param IdentityId $identityId
148
     * @param Institution $raInstitution
149
     * @return void
150
     */
151
    public function removeByIdentityIdAndInstitution(IdentityId $identityId, Institution $raInstitution)
152
    {
153
        $this->getEntityManager()->createQueryBuilder()
154
            ->delete($this->_entityName, 'ral')
155
            ->where('ral.identityId = :identityId')
156
            ->andWhere('rl.ra_institution = :institution')
157
            ->setParameter('identityId', $identityId->getIdentityId())
158
            ->setParameter('institution', $raInstitution)
159
            ->getQuery()
160
            ->execute();
161
    }
162
}
163