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 Doctrine\DBAL\Types\Type; |
22
|
|
|
use Doctrine\ORM\EntityRepository; |
23
|
|
|
use Doctrine\ORM\Query; |
24
|
|
|
use Surfnet\Stepup\Exception\RuntimeException; |
25
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type\SecondFactorStatusType; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaSecondFactor; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\RaSecondFactorQuery; |
29
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\SecondFactorStatus; |
30
|
|
|
|
31
|
|
|
class RaSecondFactorRepository extends EntityRepository |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @param string $id |
35
|
|
|
* @return RaSecondFactor|null |
36
|
|
|
*/ |
37
|
|
|
public function find($id) |
38
|
|
|
{ |
39
|
|
|
/** @var RaSecondFactor|null $secondFactor */ |
40
|
|
|
$secondFactor = parent::find($id); |
41
|
|
|
|
42
|
|
|
return $secondFactor; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $identityId |
47
|
|
|
* @return RaSecondFactor[] |
48
|
|
|
*/ |
49
|
|
|
public function findByIdentityId($identityId) |
50
|
|
|
{ |
51
|
|
|
return parent::findBy(['identityId' => $identityId]); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) The amount of if statements do not necessarily make the method |
56
|
|
|
* below complex or hard to maintain. |
57
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
58
|
|
|
* |
59
|
|
|
* @param RaSecondFactorQuery $query |
60
|
|
|
* @return Query |
61
|
|
|
*/ |
62
|
|
|
public function createSearchQuery(RaSecondFactorQuery $query) |
63
|
|
|
{ |
64
|
|
|
$queryBuilder = $this |
65
|
|
|
->createQueryBuilder('sf') |
66
|
|
|
->andWhere('sf.institution = :institution') |
67
|
|
|
->setParameter('institution', $query->institution); |
68
|
|
|
|
69
|
|
|
if ($query->name) { |
70
|
|
|
$queryBuilder->andWhere('sf.name LIKE :name')->setParameter('name', sprintf('%%%s%%', $query->name)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($query->type) { |
74
|
|
|
$queryBuilder->andWhere('sf.type = :type')->setParameter('type', $query->type); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($query->secondFactorId) { |
78
|
|
|
$queryBuilder |
79
|
|
|
->andWhere('sf.secondFactorId = :secondFactorId') |
80
|
|
|
->setParameter('secondFactorId', $query->secondFactorId); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($query->email) { |
84
|
|
|
$queryBuilder->andWhere('sf.email LIKE :email')->setParameter('email', sprintf('%%%s%%', $query->email)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($query->status) { |
88
|
|
|
$stringStatus = $query->status; |
89
|
|
|
if (!SecondFactorStatus::isValidStatus($stringStatus)) { |
90
|
|
|
throw new RuntimeException(sprintf( |
91
|
|
|
'Received invalid status "%s" in RaSecondFactorRepository::createSearchQuery', |
92
|
|
|
is_object($stringStatus) ? get_class($stringStatus) : (string) $stringStatus |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// we need to resolve the string value to database value using the correct doctrine type. Normally this is |
97
|
|
|
// done by doctrine itself, however the queries PagerFanta creates somehow manages to mangle this... |
98
|
|
|
// so we do it by hand |
99
|
|
|
$doctrineType = Type::getType(SecondFactorStatusType::NAME); |
100
|
|
|
$secondFactorStatus = SecondFactorStatus::$stringStatus(); |
101
|
|
|
|
102
|
|
|
$databaseValue = $doctrineType->convertToDatabaseValue( |
103
|
|
|
$secondFactorStatus, |
104
|
|
|
$this->getEntityManager()->getConnection()->getDatabasePlatform() |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$queryBuilder->andWhere('sf.status = :status')->setParameter('status', $databaseValue); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
switch ($query->orderBy) { |
111
|
|
|
case 'name': |
112
|
|
|
case 'type': |
113
|
|
|
case 'secondFactorId': |
114
|
|
|
case 'email': |
115
|
|
|
case 'status': |
116
|
|
|
$queryBuilder->orderBy( |
117
|
|
|
sprintf('sf.%s', $query->orderBy), |
118
|
|
|
$query->orderDirection === 'desc' ? 'DESC' : 'ASC' |
119
|
|
|
); |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $queryBuilder->getQuery(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param IdentityId $identityId |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function removeByIdentityId(IdentityId $identityId) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this->getEntityManager()->createQueryBuilder() |
133
|
|
|
->delete($this->_entityName, 'rasf') |
134
|
|
|
->where('rasf.identityId = :identityId') |
135
|
|
|
->setParameter('identityId', $identityId->getIdentityId()) |
136
|
|
|
->getQuery() |
137
|
|
|
->execute(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function save(RaSecondFactor $secondFactor) |
141
|
|
|
{ |
142
|
|
|
$this->getEntityManager()->persist($secondFactor); |
143
|
|
|
$this->getEntityManager()->flush(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param RaSecondFactor[] $secondFactors |
148
|
|
|
*/ |
149
|
|
|
public function saveAll(array $secondFactors) |
150
|
|
|
{ |
151
|
|
|
$entityManager = $this->getEntityManager(); |
152
|
|
|
|
153
|
|
|
foreach ($secondFactors as $secondFactor) { |
154
|
|
|
$entityManager->persist($secondFactor); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$entityManager->flush(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.