Completed
Push — feature/remove-institution-swi... ( c4ea05...8044ae )
by
unknown
02:42
created

createSearchForIdentityQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
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 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\VerifiedSecondFactorOfIdentityQuery;
31
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorQuery;
32
33
class VerifiedSecondFactorRepository extends EntityRepository
34
{
35
    /**
36
     * @var InstitutionAuthorizationRepositoryFilter
37
     */
38
    private $authorizationRepositoryFilter;
39
40
    /**
41
     * VerifiedSecondFactorRepository constructor.
42
     * @param EntityManager $em
43
     * @param Mapping\ClassMetadata $class
44
     * @param InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
45
     */
46
    public function __construct(
47
        EntityManager $em,
48
        Mapping\ClassMetadata $class,
49
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
50
    ) {
51
        parent::__construct($em, $class);
52
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
53
    }
54
55
56
    /**
57
     * @param string $id
58
     * @return VerifiedSecondFactor|null
59
     */
60
    public function find($id)
61
    {
62
        /** @var VerifiedSecondFactor|null $secondFactor */
63
        $secondFactor = parent::find($id);
64
65
        return $secondFactor;
66
    }
67
68
    /**
69
     * @param DateTime $requestedAt
70
     * @return VerifiedSecondFactor[]
71
     */
72
    public function findByDate(DateTime $requestedAt)
73
    {
74
        $fromDate = clone $requestedAt;
75
        $fromDate->setTime(0, 0, 0);
76
77
        $toDate = clone $requestedAt;
78
        $toDate->setTime(23, 59, 59);
79
80
        return $this->createQueryBuilder('sf')
81
            ->where('sf.registrationRequestedAt <= :toDate')
82
            ->andWhere('sf.registrationRequestedAt >= :fromDate')
83
            ->setParameter('toDate', $toDate)
84
            ->setParameter('fromDate', $fromDate)
85
            ->getQuery()
86
            ->getResult();
87
    }
88
89
    /**
90
     * @param VerifiedSecondFactorQuery $query
91
     * @return Query
92
     */
93
    public function createSearchQuery(VerifiedSecondFactorQuery $query)
94
    {
95
        $queryBuilder = $this->createQueryBuilder('sf');
96
97
        if ($query->identityId) {
98
            $queryBuilder
99
                ->andWhere('sf.identityId = :identityId')
100
                ->setParameter('identityId', (string) $query->identityId);
101
        }
102
103
        if ($query->secondFactorId) {
104
            $queryBuilder
105
                ->andWhere('sf.id = :secondFactorId')
106
                ->setParameter('secondFactorId', (string) $query->secondFactorId);
107
        }
108
109
        if (is_string($query->registrationCode)) {
110
            $queryBuilder
111
                ->andWhere('sf.registrationCode = :registrationCode')
112
                ->setParameter('registrationCode', $query->registrationCode);
113
        }
114
115
        if (!$query->authorizationContext->isActorSraa()) {
116
            // Modify query to filter on authorization
117
            $this->authorizationRepositoryFilter->filter(
118
                $queryBuilder,
119
                $query->authorizationContext,
120
                'sf.institution',
121
                'iac'
122
            );
123
        }
124
125
        return $queryBuilder->getQuery();
126
    }
127
128
    /**
129
     * @param VerifiedSecondFactorOfIdentityQuery $query
130
     * @return Query
131
     */
132 View Code Duplication
    public function createSearchForIdentityQuery(VerifiedSecondFactorOfIdentityQuery $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...
133
    {
134
        $queryBuilder = $this->createQueryBuilder('sf');
135
136
        $queryBuilder
137
            ->andWhere('sf.identityId = :identityId')
138
            ->setParameter('identityId', (string) $query->identityId);
139
140
        return $queryBuilder->getQuery();
141
    }
142
143
    public function removeByIdentityId(IdentityId $identityId)
144
    {
145
        $this->getEntityManager()->createQueryBuilder()
146
            ->delete($this->_entityName, 'sf')
147
            ->where('sf.identityId = :identityId')
148
            ->setParameter('identityId', $identityId->getIdentityId())
149
            ->getQuery()
150
            ->execute();
151
    }
152
153
    /**
154
     * @param VerifiedSecondFactor $secondFactor
155
     */
156
    public function save(VerifiedSecondFactor $secondFactor)
157
    {
158
        $this->getEntityManager()->persist($secondFactor);
159
        $this->getEntityManager()->flush();
160
    }
161
162
    public function remove(VerifiedSecondFactor $secondFactor)
163
    {
164
        $this->getEntityManager()->remove($secondFactor);
165
        $this->getEntityManager()->flush();
166
    }
167
}
168