Completed
Push — feature/fga-token-endpoints ( 83bebf )
by Michiel
34:39
created

RaSecondFactorRepository   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 147
Duplicated Lines 6.12 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 11
dl 9
loc 147
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A find() 0 7 1
A findByIdentityId() 0 4 1
D createSearchQuery() 0 65 15
A removeByIdentityId() 9 9 1
A save() 0 5 1
A saveAll() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     * @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method
74
     *                                               below complex or hard to maintain.
75
     * @SuppressWarnings(PHPMD.NPathComplexity)
76
     *
77
     * @param RaSecondFactorQuery $query
78
     * @return Query
79
     * @throws \Doctrine\DBAL\DBALException
80
     */
81
    public function createSearchQuery(RaSecondFactorQuery $query)
82
    {
83
        $queryBuilder = $this
84
            ->createQueryBuilder('sf');
85
86
        // Modify query to filter on authorization
87
        $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'sf.id', 'sf.institution', 'iac');
88
89
        if ($query->name) {
90
            $queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name));
91
        }
92
93
        if ($query->type) {
94
            $queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type);
95
        }
96
97
        if ($query->secondFactorId) {
98
            $queryBuilder
99
                ->andWhere('sf.secondFactorId = :secondFactorId')
100
                ->setParameter('secondFactorId', $query->secondFactorId);
101
        }
102
103
        if ($query->email) {
104
            $queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email));
105
        }
106
107
        if ($query->status) {
108
            $stringStatus = $query->status;
109
            if (!SecondFactorStatus::isValidStatus($stringStatus)) {
110
                throw new RuntimeException(sprintf(
111
                    'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery',
112
                    is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus
113
                ));
114
            }
115
116
            // we need to resolve the string value to database value using the correct doctrine type. Normally this is
117
            // done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this...
118
            // so we do it by hand
119
            $doctrineType = Type::getType(SecondFactorStatusType::NAME);
120
            $secondFactorStatus = SecondFactorStatus::$stringStatus();
121
122
            $databaseValue = $doctrineType->convertToDatabaseValue(
123
                $secondFactorStatus,
124
                $this->getEntityManager()->getConnection()->getDatabasePlatform()
125
            );
126
127
            $queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue);
128
        }
129
130
        switch ($query->orderBy) {
131
            case 'name':
132
            case 'type':
133
            case 'secondFactorId':
134
            case 'email':
135
            case 'institution':
136
            case 'status':
137
                $queryBuilder->orderBy(
138
                    sprintf('sf.%s', $query->orderBy),
139
                    $query->orderDirection === 'desc' ? 'DESC' : 'ASC'
140
                );
141
                break;
142
        }
143
144
        return $queryBuilder->getQuery();
145
    }
146
147
    /**
148
     * @param IdentityId $identityId
149
     * @return void
150
     */
151 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...
152
    {
153
        $this->getEntityManager()->createQueryBuilder()
154
            ->delete($this->_entityName, 'rasf')
155
            ->where('rasf.identityId = :identityId')
156
            ->setParameter('identityId', $identityId->getIdentityId())
157
            ->getQuery()
158
            ->execute();
159
    }
160
161
    public function save(RaSecondFactor $secondFactor)
162
    {
163
        $this->getEntityManager()->persist($secondFactor);
164
        $this->getEntityManager()->flush();
165
    }
166
167
    /**
168
     * @param RaSecondFactor[] $secondFactors
169
     */
170
    public function saveAll(array $secondFactors)
171
    {
172
        $entityManager = $this->getEntityManager();
173
174
        foreach ($secondFactors as $secondFactor) {
175
            $entityManager->persist($secondFactor);
176
        }
177
178
        $entityManager->flush();
179
    }
180
}
181