Completed
Push — develop ( 800b48...35e2e2 )
by Michiel
13s
created

RaSecondFactorRepository::findByInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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\DBAL\Types\Type;
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\EntityRepository;
24
use Doctrine\ORM\Mapping;
25
use Doctrine\ORM\Query;
26
use Surfnet\Stepup\Exception\RuntimeException;
27
use Surfnet\Stepup\Identity\Value\IdentityId;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
29
use Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\SecondFactorStatusType;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaSecondFactor;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaSecondFactorQuery;
32
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\SecondFactorStatus;
33
34
class RaSecondFactorRepository extends EntityRepository
35
{
36
    /**
37
     * @var InstitutionAuthorizationRepositoryFilter
38
     */
39
    private $authorizationRepositoryFilter;
40
41
    public function __construct(
42
        EntityManager $em,
43
        Mapping\ClassMetadata $class,
44
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
45
    ) {
46
        parent::__construct($em, $class);
47
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
48
    }
49
50
51
    /**
52
     * @param string $id
53
     * @return RaSecondFactor|null
54
     */
55
    public function find($id)
56
    {
57
        /** @var RaSecondFactor|null $secondFactor */
58
        $secondFactor = parent::find($id);
59
60
        return $secondFactor;
61
    }
62
63
    /**
64
     * @param string $identityId
65
     * @return RaSecondFactor[]
66
     */
67
    public function findByIdentityId($identityId)
68
    {
69
        return parent::findBy(['identityId' => $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...
70
    }
71
72
73
    /**
74
     * @param string $institution
75
     * @return RaSecondFactor[]
76
     */
77
    public function findByInstitution($institution)
78
    {
79
        return parent::findBy(['institution' => $institution]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findBy() instead of findByInstitution()). 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...
80
    }
81
82
    /**
83
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
84
     *                                               below complex or hard to maintain.
85
     * @SuppressWarnings(PHPMD.NPathComplexity)
86
     *
87
     * @param RaSecondFactorQuery $query
88
     * @return Query
89
     * @throws \Doctrine\DBAL\DBALException
90
     */
91
    public function createSearchQuery(RaSecondFactorQuery $query)
92
    {
93
        $queryBuilder = $this
94
            ->createQueryBuilder('sf');
95
96
        // Modify query to filter on authorization
97
        // The SRAA user does not adhere to the FGA filter rules when searching for tokens
98
        if (!$query->authorizationContext->isActorSraa()) {
99
            $this->authorizationRepositoryFilter->filter(
100
                $queryBuilder,
101
                $query->authorizationContext,
102
                'sf.institution',
103
                'iac'
104
            );
105
        }
106
107
        if ($query->name) {
108
            $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name));
109
        }
110
111
        if ($query->type) {
112
            $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type);
113
        }
114
115
        if ($query->secondFactorId) {
116
            $queryBuilder
117
                ->andWhere('sf.secondFactorId = :secondFactorId')
118
                ->setParameter('secondFactorId', $query->secondFactorId);
119
        }
120
121
        if ($query->email) {
122
            $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email));
123
        }
124
125
        if ($query->institution) {
126
            $queryBuilder->andWhere('sf.institution = :institution')->setParameter('institution', $query->institution);
127
        }
128
129
        if ($query->status) {
130
            $stringStatus = $query->status;
131
            if (!SecondFactorStatus::isValidStatus($stringStatus)) {
132
                throw new RuntimeException(sprintf(
133
                    'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery',
134
                    is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus
135
                ));
136
            }
137
138
            // we need to resolve the string value to database value using the correct doctrine type. Normally this is
139
            // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this...
140
            // so we do it by hand
141
            $doctrineType = Type::getType(SecondFactorStatusType::NAME);
142
            $secondFactorStatus = SecondFactorStatus::$stringStatus();
143
144
            $databaseValue = $doctrineType->convertToDatabaseValue(
145
                $secondFactorStatus,
146
                $this->getEntityManager()->getConnection()->getDatabasePlatform()
147
            );
148
149
            $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue);
150
        }
151
152
        switch ($query->orderBy) {
153
            case 'name':
154
            case 'type':
155
            case 'secondFactorId':
156
            case 'email':
157
            case 'institution':
158
            case 'status':
159
                $queryBuilder->orderBy(
160
                    sprintf('sf.%s', $query->orderBy),
161
                    $query->orderDirection === 'desc' ? 'DESC' : 'ASC'
162
                );
163
                break;
164
        }
165
166
        return $queryBuilder->getQuery();
167
    }
168
169
    /**
170
     * @param IdentityId $identityId
171
     * @return void
172
     */
173 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...
174
    {
175
        $this->getEntityManager()->createQueryBuilder()
176
            ->delete($this->_entityName, 'rasf')
177
            ->where('rasf.identityId = :identityId')
178
            ->setParameter('identityId', $identityId->getIdentityId())
179
            ->getQuery()
180
            ->execute();
181
    }
182
183
    public function save(RaSecondFactor $secondFactor)
184
    {
185
        $this->getEntityManager()->persist($secondFactor);
186
        $this->getEntityManager()->flush();
187
    }
188
189
    /**
190
     * @param RaSecondFactor[] $secondFactors
191
     */
192
    public function saveAll(array $secondFactors)
193
    {
194
        $entityManager = $this->getEntityManager();
195
196
        foreach ($secondFactors as $secondFactor) {
197
            $entityManager->persist($secondFactor);
198
        }
199
200
        $entityManager->flush();
201
    }
202
}
203