Completed
Pull Request — feature/fine-grained-authoriza... (#242)
by
unknown
05:24 queued 02:46
created

VerifiedSecondFactorRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 31.3 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 36
loc 115
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 findByDate() 0 16 1
A createSearchQuery() 27 27 4
A removeByIdentityId() 9 9 1
A save() 0 5 1
A remove() 0 5 1

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 DateTime;
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\EntityRepository;
24
use Doctrine\ORM\Mapping;
25
use Doctrine\ORM\Query;
26
use Surfnet\Stepup\Identity\Value\IdentityId;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Value\InstitutionAuthorizationContextInterface;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VerifiedSecondFactor;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorQuery;
31
32
class VerifiedSecondFactorRepository extends EntityRepository
33
{
34
    /**
35
     * @var InstitutionAuthorizationRepositoryFilter
36
     */
37
    private $authorizationRepositoryFilter;
38
39
    /**
40
     * VerifiedSecondFactorRepository constructor.
41
     * @param EntityManager $em
42
     * @param Mapping\ClassMetadata $class
43
     * @param InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
44
     */
45
    public function __construct(
46
        EntityManager $em,
47
        Mapping\ClassMetadata $class,
48
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
49
    ) {
50
        parent::__construct($em, $class);
51
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
52
    }
53
54
55
    /**
56
     * @param string $id
57
     * @return VerifiedSecondFactor|null
58
     */
59
    public function find($id)
60
    {
61
        /** @var VerifiedSecondFactor|null $secondFactor */
62
        $secondFactor = parent::find($id);
63
64
        return $secondFactor;
65
    }
66
67
    /**
68
     * @param DateTime $requestedAt
69
     * @return VerifiedSecondFactor[]
70
     */
71
    public function findByDate(DateTime $requestedAt)
72
    {
73
        $fromDate = clone $requestedAt;
74
        $fromDate->setTime(0, 0, 0);
75
76
        $toDate = clone $requestedAt;
77
        $toDate->setTime(23, 59, 59);
78
79
        return $this->createQueryBuilder('sf')
80
            ->where('sf.registrationRequestedAt <= :toDate')
81
            ->andWhere('sf.registrationRequestedAt >= :fromDate')
82
            ->setParameter('toDate', $toDate)
83
            ->setParameter('fromDate', $fromDate)
84
            ->getQuery()
85
            ->getResult();
86
    }
87
88
    /**
89
     * @param VerifiedSecondFactorQuery $query
90
     * @param InstitutionAuthorizationContextInterface $authorizationContext
0 ignored issues
show
Bug introduced by
There is no parameter named $authorizationContext. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
91
     * @param InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
0 ignored issues
show
Bug introduced by
There is no parameter named $authorizationRepositoryFilter. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
92
     * @return Query
93
     */
94 View Code Duplication
    public function createSearchQuery(VerifiedSecondFactorQuery $query)
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...
95
    {
96
        $queryBuilder = $this->createQueryBuilder('sf');
97
98
        // Modify query to filter on authorization
99
        $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'sf.id', 'sf.institution', 'iac');
100
101
        if ($query->identityId) {
102
            $queryBuilder
103
                ->andWhere('sf.identityId = :identityId')
104
                ->setParameter('identityId', (string) $query->identityId);
105
        }
106
107
        if ($query->secondFactorId) {
108
            $queryBuilder
109
                ->andWhere('sf.id = :secondFactorId')
110
                ->setParameter('secondFactorId', (string) $query->secondFactorId);
111
        }
112
113
        if (is_string($query->registrationCode)) {
114
            $queryBuilder
115
                ->andWhere('sf.registrationCode = :registrationCode')
116
                ->setParameter('registrationCode', $query->registrationCode);
117
        }
118
119
        return $queryBuilder->getQuery();
120
    }
121
122 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...
123
    {
124
        $this->getEntityManager()->createQueryBuilder()
125
            ->delete($this->_entityName, 'sf')
126
            ->where('sf.identityId = :identityId')
127
            ->setParameter('identityId', $identityId->getIdentityId())
128
            ->getQuery()
129
            ->execute();
130
    }
131
132
    /**
133
     * @param VerifiedSecondFactor $secondFactor
134
     */
135
    public function save(VerifiedSecondFactor $secondFactor)
136
    {
137
        $this->getEntityManager()->persist($secondFactor);
138
        $this->getEntityManager()->flush();
139
    }
140
141
    public function remove(VerifiedSecondFactor $secondFactor)
142
    {
143
        $this->getEntityManager()->remove($secondFactor);
144
        $this->getEntityManager()->flush();
145
    }
146
}
147