1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2022 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\Doctrine\Type; |
20
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
22
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
23
|
|
|
use Doctrine\DBAL\Types\Type; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RecoveryTokenStatus; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Custom Doctrine Type for the four possible statuses a recovery token |
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class RecoveryTokenStatusType extends Type |
30
|
|
|
{ |
31
|
|
|
public const NAME = 'stepup_recovery_token_status'; |
32
|
|
|
|
33
|
|
|
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
34
|
|
|
{ |
35
|
|
|
return $platform->getIntegerTypeDeclarationSQL($column); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @throws ConversionException |
40
|
|
|
*/ |
|
|
|
|
41
|
|
|
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): int |
42
|
|
|
{ |
43
|
|
|
if (!$value instanceof RecoveryTokenStatus) { |
44
|
|
|
throw new ConversionException( |
45
|
|
|
sprintf( |
46
|
|
|
"Encountered illegal recovery token status of type %s '%s', expected a RecoveryTokenStatus instance", |
47
|
|
|
get_debug_type($value), |
48
|
|
|
is_scalar($value) ? (string)$value : '', |
49
|
|
|
), |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (RecoveryTokenStatus::active()->equals($value)) { |
54
|
|
|
return 0; |
55
|
|
|
} elseif (RecoveryTokenStatus::revoked()->equals($value)) { |
56
|
|
|
return 10; |
57
|
|
|
} elseif (RecoveryTokenStatus::forgotten()->equals($value)) { |
58
|
|
|
return 20; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new ConversionException(sprintf("Encountered inconvertible second factor status '%s'", (string)$value)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @throws ConversionException |
66
|
|
|
*/ |
|
|
|
|
67
|
|
|
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): RecoveryTokenStatus |
68
|
|
|
{ |
69
|
|
|
if (is_scalar($value)) { |
70
|
|
|
$value = (string)$value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($value === '0') { |
74
|
|
|
return RecoveryTokenStatus::active(); |
75
|
|
|
} elseif ($value === '10') { |
76
|
|
|
return RecoveryTokenStatus::revoked(); |
77
|
|
|
} elseif ($value === '20') { |
78
|
|
|
return RecoveryTokenStatus::forgotten(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new ConversionException( |
82
|
|
|
sprintf( |
83
|
|
|
"Encountered illegal recovery token status of type %s '%s', expected it to be one of [0,10,20]", |
84
|
|
|
get_debug_type($value), |
85
|
|
|
is_scalar($value) ? (string)$value : '', |
86
|
|
|
), |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getName(): string |
91
|
|
|
{ |
92
|
|
|
return self::NAME; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|