VerifiedSecondFactorRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

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

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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VerifiedSecondFactor;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorOfIdentityQuery;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\VerifiedSecondFactorQuery;
30
31
/**
32
 * @extends ServiceEntityRepository<VerifiedSecondFactor>
33
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
34
class VerifiedSecondFactorRepository extends ServiceEntityRepository
35
{
36
    public function __construct(
37
        ManagerRegistry $registry,
38
        private readonly InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter,
39
    ) {
40
        parent::__construct($registry, VerifiedSecondFactor::class);
41
    }
42
43
    public function find(mixed $id, $lockMode = null, $lockVersion = null): ?VerifiedSecondFactor
44
    {
45
        /** @var VerifiedSecondFactor|null $secondFactor */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
46
        $secondFactor = parent::find($id);
47
48
        return $secondFactor;
49
    }
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $requestedAt should have a doc-comment as per coding-style.
Loading history...
52
     * @return VerifiedSecondFactor[]
53
     */
54
    public function findByDate(DateTime $requestedAt): array
55
    {
56
        $fromDate = clone $requestedAt;
57
        $fromDate->setTime(0, 0);
58
59
        $toDate = clone $requestedAt;
60
        $toDate->setTime(23, 59, 59);
61
62
        return $this->createQueryBuilder('sf')
63
            ->where('sf.registrationRequestedAt <= :toDate')
64
            ->andWhere('sf.registrationRequestedAt >= :fromDate')
65
            ->setParameter('toDate', $toDate)
66
            ->setParameter('fromDate', $fromDate)
67
            ->getQuery()
68
            ->getResult();
69
    }
70
71
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
72
     * @return Query
73
     */
74
    public function createSearchQuery(VerifiedSecondFactorQuery $query): Query
75
    {
76
        $queryBuilder = $this->createQueryBuilder('sf');
77
78
        if ($query->identityId instanceof \Surfnet\Stepup\Identity\Value\IdentityId) {
79
            $queryBuilder
80
                ->andWhere('sf.identityId = :identityId')
81
                ->setParameter('identityId', (string)$query->identityId);
82
        }
83
84
        if ($query->secondFactorId instanceof \Surfnet\Stepup\Identity\Value\SecondFactorId) {
85
            $queryBuilder
86
                ->andWhere('sf.id = :secondFactorId')
87
                ->setParameter('secondFactorId', (string)$query->secondFactorId);
88
        }
89
90
        if (is_string($query->registrationCode)) {
91
            $queryBuilder
92
                ->andWhere('sf.registrationCode = :registrationCode')
93
                ->setParameter('registrationCode', $query->registrationCode);
94
        }
95
96
        // Modify query to filter on authorization:
97
        // We want to list all second factors of the institution we are RA for.
98
        $this->authorizationRepositoryFilter->filter(
99
            $queryBuilder,
100
            $query->authorizationContext,
101
            'sf.institution',
102
            'iac',
103
        );
104
105
        return $queryBuilder->getQuery();
106
    }
107
108
    public function createSearchForIdentityQuery(VerifiedSecondFactorOfIdentityQuery $query): Query
109
    {
110
        $queryBuilder = $this->createQueryBuilder('sf');
111
112
        $queryBuilder
113
            ->andWhere('sf.identityId = :identityId')
114
            ->setParameter('identityId', (string)$query->identityId);
115
116
        return $queryBuilder->getQuery();
117
    }
118
119
    public function removeByIdentityId(IdentityId $identityId): void
120
    {
121
        $this->getEntityManager()->createQueryBuilder()
122
            ->delete($this->getEntityName(), 'sf')
123
            ->where('sf.identityId = :identityId')
124
            ->setParameter('identityId', $identityId->getIdentityId())
125
            ->getQuery()
126
            ->execute();
127
    }
128
129
    public function save(VerifiedSecondFactor $secondFactor): void
130
    {
131
        $this->getEntityManager()->persist($secondFactor);
132
        $this->getEntityManager()->flush();
133
    }
134
135
    public function remove(VerifiedSecondFactor $secondFactor): void
136
    {
137
        $this->getEntityManager()->remove($secondFactor);
138
        $this->getEntityManager()->flush();
139
    }
140
}
141