VerifiedSecondFactorRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 49
c 1
b 0
f 0
dl 0
loc 102
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createSearchForIdentityQuery() 0 9 1
A createSearchQuery() 0 32 4
A find() 0 6 1
A findByDate() 0 15 1
A removeByIdentityId() 0 8 1
A __construct() 0 5 1
A remove() 0 4 1
A save() 0 4 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\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
23
use Doctrine\ORM\Query;
24
use Doctrine\Persistence\ManagerRegistry;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\SecondFactorId;
27
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VerifiedSecondFactor;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorOfIdentityQuery;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorQuery;
31
32
/**
33
 * @extends ServiceEntityRepository<VerifiedSecondFactor>
34
 */
35
class VerifiedSecondFactorRepository extends ServiceEntityRepository
36
{
37
    public function __construct(
38
        ManagerRegistry $registry,
39
        private readonly InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter,
40
    ) {
41
        parent::__construct($registry, VerifiedSecondFactor::class);
42
    }
43
44
    public function find(mixed $id, $lockMode = null, $lockVersion = null): ?VerifiedSecondFactor
45
    {
46
        /** @var VerifiedSecondFactor|null $secondFactor */
47
        $secondFactor = parent::find($id);
48
49
        return $secondFactor;
50
    }
51
52
    /**
53
     * @return VerifiedSecondFactor[]
54
     */
55
    public function findByDate(DateTime $requestedAt): array
56
    {
57
        $fromDate = clone $requestedAt;
58
        $fromDate->setTime(0, 0);
59
60
        $toDate = clone $requestedAt;
61
        $toDate->setTime(23, 59, 59);
62
63
        return $this->createQueryBuilder('sf')
64
            ->where('sf.registrationRequestedAt <= :toDate')
65
            ->andWhere('sf.registrationRequestedAt >= :fromDate')
66
            ->setParameter('toDate', $toDate)
67
            ->setParameter('fromDate', $fromDate)
68
            ->getQuery()
69
            ->getResult();
70
    }
71
72
    public function createSearchQuery(VerifiedSecondFactorQuery $query): Query
73
    {
74
        $queryBuilder = $this->createQueryBuilder('sf');
75
76
        if ($query->identityId instanceof IdentityId) {
77
            $queryBuilder
78
                ->andWhere('sf.identityId = :identityId')
79
                ->setParameter('identityId', (string)$query->identityId);
80
        }
81
82
        if ($query->secondFactorId instanceof SecondFactorId) {
83
            $queryBuilder
84
                ->andWhere('sf.id = :secondFactorId')
85
                ->setParameter('secondFactorId', (string)$query->secondFactorId);
86
        }
87
88
        if (is_string($query->registrationCode)) {
89
            $queryBuilder
90
                ->andWhere('sf.registrationCode = :registrationCode')
91
                ->setParameter('registrationCode', $query->registrationCode);
92
        }
93
94
        // Modify query to filter on authorization:
95
        // We want to list all second factors of the institution we are RA for.
96
        $this->authorizationRepositoryFilter->filter(
97
            $queryBuilder,
98
            $query->authorizationContext,
99
            'sf.institution',
100
            'iac',
101
        );
102
103
        return $queryBuilder->getQuery();
104
    }
105
106
    public function createSearchForIdentityQuery(VerifiedSecondFactorOfIdentityQuery $query): Query
107
    {
108
        $queryBuilder = $this->createQueryBuilder('sf');
109
110
        $queryBuilder
111
            ->andWhere('sf.identityId = :identityId')
112
            ->setParameter('identityId', (string)$query->identityId);
113
114
        return $queryBuilder->getQuery();
115
    }
116
117
    public function removeByIdentityId(IdentityId $identityId): void
118
    {
119
        $this->getEntityManager()->createQueryBuilder()
120
            ->delete($this->getEntityName(), 'sf')
121
            ->where('sf.identityId = :identityId')
122
            ->setParameter('identityId', $identityId->getIdentityId())
123
            ->getQuery()
124
            ->execute();
125
    }
126
127
    public function save(VerifiedSecondFactor $secondFactor): void
128
    {
129
        $this->getEntityManager()->persist($secondFactor);
130
        $this->getEntityManager()->flush();
131
    }
132
133
    public function remove(VerifiedSecondFactor $secondFactor): void
134
    {
135
        $this->getEntityManager()->remove($secondFactor);
136
        $this->getEntityManager()->flush();
137
    }
138
}
139