Completed
Push — feature/fga-token-endpoints ( 7ef4ac )
by
unknown
03:07
created

RaSecondFactorRepository::find()   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 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\EntityRepository;
23
use Doctrine\ORM\Query;
24
use Surfnet\Stepup\Exception\RuntimeException;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContext;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
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
     * @param string $id
38
     * @return RaSecondFactor|null
39
     */
40
    public function find($id)
41
    {
42
        /** @var RaSecondFactor|null $secondFactor */
43
        $secondFactor = parent::find($id);
44
45
        return $secondFactor;
46
    }
47
48
    /**
49
     * @param string $identityId
50
     * @return RaSecondFactor[]
51
     */
52
    public function findByIdentityId($identityId)
53
    {
54
        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...
55
    }
56
57
    /**
58
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
59
     *                                               below complex or hard to maintain.
60
     * @SuppressWarnings(PHPMD.NPathComplexity)
61
     *
62
     * @param RaSecondFactorQuery $query
63
     * @param InstitutionAuthorizationContextInterface $authorizationContext
64
     * @param InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
65
     * @return Query
66
     * @throws \Doctrine\DBAL\DBALException
67
     */
68
    public function createSearchQuery(
69
        RaSecondFactorQuery $query,
70
        InstitutionAuthorizationContextInterface $authorizationContext,
71
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
72
    ) {
73
        $queryBuilder = $this
74
            ->createQueryBuilder('sf');
75
76
        // Modify query to filter on authorization
77
        $authorizationRepositoryFilter->filter($queryBuilder, $authorizationContext, 'sf.id', 'sf.institution', 'iac');
78
79
        if ($query->name) {
80
            $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name));
81
        }
82
83
        if ($query->type) {
84
            $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type);
85
        }
86
87
        if ($query->secondFactorId) {
88
            $queryBuilder
89
                ->andWhere('sf.secondFactorId = :secondFactorId')
90
                ->setParameter('secondFactorId', $query->secondFactorId);
91
        }
92
93
        if ($query->email) {
94
            $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email));
95
        }
96
97
        if ($query->status) {
98
            $stringStatus = $query->status;
99
            if (!SecondFactorStatus::isValidStatus($stringStatus)) {
100
                throw new RuntimeException(sprintf(
101
                    'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery',
102
                    is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus
103
                ));
104
            }
105
106
            // we need to resolve the string value to database value using the correct doctrine type. Normally this is
107
            // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this...
108
            // so we do it by hand
109
            $doctrineType = Type::getType(SecondFactorStatusType::NAME);
110
            $secondFactorStatus = SecondFactorStatus::$stringStatus();
111
112
            $databaseValue = $doctrineType->convertToDatabaseValue(
113
                $secondFactorStatus,
114
                $this->getEntityManager()->getConnection()->getDatabasePlatform()
115
            );
116
117
            $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue);
118
        }
119
120
        switch ($query->orderBy) {
121
            case 'name':
122
            case 'type':
123
            case 'secondFactorId':
124
            case 'email':
125
            case 'status':
126
                $queryBuilder->orderBy(
127
                    sprintf('sf.%s', $query->orderBy),
128
                    $query->orderDirection === 'desc' ? 'DESC' : 'ASC'
129
                );
130
                break;
131
        }
132
133
        return $queryBuilder->getQuery();
134
    }
135
136
    /**
137
     * @param IdentityId $identityId
138
     * @return void
139
     */
140 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...
141
    {
142
        $this->getEntityManager()->createQueryBuilder()
143
            ->delete($this->_entityName, 'rasf')
144
            ->where('rasf.identityId = :identityId')
145
            ->setParameter('identityId', $identityId->getIdentityId())
146
            ->getQuery()
147
            ->execute();
148
    }
149
150
    public function save(RaSecondFactor $secondFactor)
151
    {
152
        $this->getEntityManager()->persist($secondFactor);
153
        $this->getEntityManager()->flush();
154
    }
155
156
    /**
157
     * @param RaSecondFactor[] $secondFactors
158
     */
159
    public function saveAll(array $secondFactors)
160
    {
161
        $entityManager = $this->getEntityManager();
162
163
        foreach ($secondFactors as $secondFactor) {
164
            $entityManager->persist($secondFactor);
165
        }
166
167
        $entityManager->flush();
168
    }
169
}
170