Completed
Push — feature/remove-institution-swi... ( 073a79...c4ea05 )
by
unknown
02:51
created

createSearchQuery()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.0648
c 0
b 0
f 0
cc 5
nc 16
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\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
     * @return Query
91
     */
92
    public function createSearchQuery(VerifiedSecondFactorQuery $query)
93
    {
94
        $queryBuilder = $this->createQueryBuilder('sf');
95
96
        if ($query->identityId) {
97
            $queryBuilder
98
                ->andWhere('sf.identityId = :identityId')
99
                ->setParameter('identityId', (string) $query->identityId);
100
        }
101
102
        if ($query->secondFactorId) {
103
            $queryBuilder
104
                ->andWhere('sf.id = :secondFactorId')
105
                ->setParameter('secondFactorId', (string) $query->secondFactorId);
106
        }
107
108
        if (is_string($query->registrationCode)) {
109
            $queryBuilder
110
                ->andWhere('sf.registrationCode = :registrationCode')
111
                ->setParameter('registrationCode', $query->registrationCode);
112
        }
113
114
        if (!$query->authorizationContext->isActorSraa()) {
115
            // Modify query to filter on authorization
116
            $this->authorizationRepositoryFilter->filter(
117
                $queryBuilder,
118
                $query->authorizationContext,
119
                'sf.institution',
120
                'iac'
121
            );
122
        }
123
124
        return $queryBuilder->getQuery();
125
    }
126
127
    public function removeByIdentityId(IdentityId $identityId)
128
    {
129
        $this->getEntityManager()->createQueryBuilder()
130
            ->delete($this->_entityName, 'sf')
131
            ->where('sf.identityId = :identityId')
132
            ->setParameter('identityId', $identityId->getIdentityId())
133
            ->getQuery()
134
            ->execute();
135
    }
136
137
    /**
138
     * @param VerifiedSecondFactor $secondFactor
139
     */
140
    public function save(VerifiedSecondFactor $secondFactor)
141
    {
142
        $this->getEntityManager()->persist($secondFactor);
143
        $this->getEntityManager()->flush();
144
    }
145
146
    public function remove(VerifiedSecondFactor $secondFactor)
147
    {
148
        $this->getEntityManager()->remove($secondFactor);
149
        $this->getEntityManager()->flush();
150
    }
151
}
152