Skip to content

Commit 061aac4

Browse files
kayjoostenjohanib
authored andcommitted
refactor: convert ConsentType to a PHP 8.1 backed enum (#1929)
* refactor: convert ConsentType to a PHP 8.1 backed enum Replace the ConsentType value class (with its deprecated public constructor since ~2015) with a proper backed enum. - ConsentType is now `enum ConsentType: string` with cases Explicit = 'explicit' and Implicit = 'implicit' - The deprecated public constructor, the explicit()/implicit() named constructors and the equals() method are removed - DbalConsentRepository uses ConsentType::from() to hydrate from DB - Legacy Consent model methods now accept ConsentType and pass ->value into SQL parameter arrays - All call sites updated from TYPE_EXPLICIT/TYPE_IMPLICIT constants and named constructors to enum cases - ConsentTypeTest updated: invalid-string rejection tested via from(), equality via assertSame() on enum singletons
1 parent 7765e49 commit 061aac4

9 files changed

Lines changed: 45 additions & 177 deletions

File tree

library/EngineBlock/Corto/Model/Consent.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function explicitConsentWasGivenFor(ServiceProvider $serviceProvider): Co
9191
// Consent disabled: treat as already given (stable — no upgrade needed)
9292
return ConsentVersion::stable();
9393
}
94-
return $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_EXPLICIT);
94+
return $this->_hasStoredConsent($serviceProvider, ConsentType::Explicit);
9595
}
9696

9797
/**
@@ -102,7 +102,7 @@ public function explicitConsentWasGivenFor(ServiceProvider $serviceProvider): Co
102102
* The caller must pass the ConsentVersion already retrieved by explicitConsentWasGivenFor or
103103
* implicitConsentWasGivenFor to avoid a second identical DB query.
104104
*/
105-
public function upgradeAttributeHashFor(ServiceProvider $serviceProvider, string $consentType, ConsentVersion $consentVersion): void
105+
public function upgradeAttributeHashFor(ServiceProvider $serviceProvider, ConsentType $consentType, ConsentVersion $consentVersion): void
106106
{
107107
if (!$this->_consentEnabled) {
108108
return;
@@ -117,19 +117,19 @@ public function implicitConsentWasGivenFor(ServiceProvider $serviceProvider): Co
117117
if (!$this->_consentEnabled) {
118118
return ConsentVersion::stable();
119119
}
120-
return $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_IMPLICIT);
120+
return $this->_hasStoredConsent($serviceProvider, ConsentType::Implicit);
121121
}
122122

123123
public function giveExplicitConsentFor(ServiceProvider $serviceProvider): bool
124124
{
125125
return !$this->_consentEnabled ||
126-
$this->_storeConsent($serviceProvider, ConsentType::TYPE_EXPLICIT);
126+
$this->_storeConsent($serviceProvider, ConsentType::Explicit);
127127
}
128128

129129
public function giveImplicitConsentFor(ServiceProvider $serviceProvider): bool
130130
{
131131
return !$this->_consentEnabled ||
132-
$this->_storeConsent($serviceProvider, ConsentType::TYPE_IMPLICIT);
132+
$this->_storeConsent($serviceProvider, ConsentType::Implicit);
133133
}
134134

135135
public function countTotalConsent(): int
@@ -157,7 +157,7 @@ protected function _getStableAttributesHash($attributes): string
157157
return $this->_hashService->getStableAttributesHash($attributes, $this->_mustStoreValues);
158158
}
159159

160-
private function _storeConsent(ServiceProvider $serviceProvider, $consentType): bool
160+
private function _storeConsent(ServiceProvider $serviceProvider, ConsentType $consentType): bool
161161
{
162162
$consentUuid = $this->_getConsentUid();
163163
if (!is_string($consentUuid)) {
@@ -168,14 +168,14 @@ private function _storeConsent(ServiceProvider $serviceProvider, $consentType):
168168
hashedUserId: sha1($consentUuid),
169169
serviceId: $serviceProvider->entityId,
170170
attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes),
171-
consentType: $consentType,
171+
consentType: $consentType->value,
172172
attributeHash: $this->_getAttributesHash($this->_responseAttributes),
173173
);
174174

175175
return $this->_hashService->storeConsentHash($parameters);
176176
}
177177

178-
private function _updateConsent(ServiceProvider $serviceProvider, $consentType): bool
178+
private function _updateConsent(ServiceProvider $serviceProvider, ConsentType $consentType): bool
179179
{
180180
$consentUid = $this->_getConsentUid();
181181
if (!is_string($consentUid)) {
@@ -187,13 +187,13 @@ private function _updateConsent(ServiceProvider $serviceProvider, $consentType):
187187
attributeHash: $this->_getAttributesHash($this->_responseAttributes),
188188
hashedUserId: sha1($consentUid),
189189
serviceId: $serviceProvider->entityId,
190-
consentType: $consentType,
190+
consentType: $consentType->value,
191191
);
192192

193193
return $this->_hashService->updateConsentHash($parameters);
194194
}
195195

196-
private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentType): ConsentVersion
196+
private function _hasStoredConsent(ServiceProvider $serviceProvider, ConsentType $consentType): ConsentVersion
197197
{
198198
$consentUid = $this->_getConsentUid();
199199
if (!is_string($consentUid)) {
@@ -205,7 +205,7 @@ private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentTyp
205205
serviceId: $serviceProvider->entityId,
206206
attributeHash: $this->_getAttributesHash($this->_responseAttributes),
207207
attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes),
208-
consentType: $consentType,
208+
consentType: $consentType->value,
209209
);
210210
return $this->_hashService->retrieveConsentHash($query);
211211
}

library/EngineBlock/Corto/Module/Service/ProcessConsent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function serve($serviceName, Request $httpRequest)
104104
if (!$explicitConsent->given()) {
105105
$consentRepository->giveExplicitConsentFor($destinationMetadata);
106106
} else {
107-
$consentRepository->upgradeAttributeHashFor($destinationMetadata, ConsentType::TYPE_EXPLICIT, $explicitConsent);
107+
$consentRepository->upgradeAttributeHashFor($destinationMetadata, ConsentType::Explicit, $explicitConsent);
108108
}
109109

110110
$response->setConsent(Constants::CONSENT_OBTAINED);

library/EngineBlock/Corto/Module/Service/ProvideConsent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function serve($serviceName, Request $httpRequest)
148148
if (!$implicitConsent->given()) {
149149
$consentRepository->giveImplicitConsentFor($serviceProviderMetadata);
150150
} else {
151-
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::TYPE_IMPLICIT, $implicitConsent);
151+
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::Implicit, $implicitConsent);
152152
}
153153

154154
$response->setConsent(Constants::CONSENT_INAPPLICABLE);
@@ -167,7 +167,7 @@ public function serve($serviceName, Request $httpRequest)
167167

168168
$priorConsent = $consentRepository->explicitConsentWasGivenFor($serviceProviderMetadata);
169169
if ($priorConsent->given()) {
170-
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::TYPE_EXPLICIT, $priorConsent);
170+
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::Explicit, $priorConsent);
171171

172172
$response->setConsent(Constants::CONSENT_PRIOR);
173173

src/OpenConext/EngineBlock/Authentication/Value/ConsentType.php

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,14 @@
1919
namespace OpenConext\EngineBlock\Authentication\Value;
2020

2121
use JsonSerializable;
22-
use OpenConext\EngineBlock\Assert\Assertion;
2322

24-
final class ConsentType implements JsonSerializable
23+
enum ConsentType: string implements JsonSerializable
2524
{
26-
const TYPE_EXPLICIT = 'explicit';
27-
const TYPE_IMPLICIT = 'implicit';
25+
case Explicit = 'explicit';
26+
case Implicit = 'implicit';
2827

29-
/**
30-
* @var string
31-
*/
32-
private $consentType;
33-
34-
/**
35-
* @return ConsentType
36-
*/
37-
public static function explicit()
38-
{
39-
return new self(self::TYPE_EXPLICIT);
40-
}
41-
42-
/**
43-
* @return ConsentType
44-
*/
45-
public static function implicit()
46-
{
47-
return new self(self::TYPE_IMPLICIT);
48-
}
49-
50-
/**
51-
* @param ConsentType::TYPE_EXPLICIT|ConsentType::TYPE_IMPLICIT $consentType
52-
*
53-
* @deprecated Use the implicit and explicit named constructors. Will be removed
54-
* when Doctrine ORM is implemented.
55-
*/
56-
public function __construct($consentType)
57-
{
58-
Assertion::choice(
59-
$consentType,
60-
[self::TYPE_EXPLICIT, self::TYPE_IMPLICIT],
61-
'ConsentType must be one of ConsentType::TYPE_EXPLICIT, ConsentType::TYPE_IMPLICIT'
62-
);
63-
64-
$this->consentType = $consentType;
65-
}
66-
67-
/**
68-
* @param ConsentType $other
69-
* @return bool
70-
*/
71-
public function equals(ConsentType $other)
72-
{
73-
return $this->consentType === $other->consentType;
74-
}
75-
76-
/**
77-
* @return string
78-
*/
79-
public function jsonSerialize(): mixed
80-
{
81-
return $this->consentType;
82-
}
83-
84-
/**
85-
* @return string
86-
*/
87-
public function __toString()
28+
public function jsonSerialize(): string
8829
{
89-
return $this->consentType;
30+
return $this->value;
9031
}
9132
}

src/OpenConext/EngineBlockBundle/Authentication/Repository/DbalConsentRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function (array $row) use ($userId) {
9696
$userId,
9797
$row['service_id'],
9898
new DateTime($row['consent_date']),
99-
new ConsentType($row['consent_type']),
99+
ConsentType::from($row['consent_type']),
100100
$row['attribute_stable'] ?? $row['attribute']
101101
);
102102
},

tests/library/EngineBlock/Test/Corto/Model/ConsentIntegrationTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public function test_no_previous_consent_given($consentType)
9595
->once()
9696
->andReturn(ConsentVersion::notGiven());
9797
switch ($consentType) {
98-
case ConsentType::TYPE_EXPLICIT:
98+
case ConsentType::Explicit:
9999
$this->assertFalse($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
100100
break;
101-
case ConsentType::TYPE_IMPLICIT:
101+
case ConsentType::Implicit:
102102
$this->assertFalse($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
103103
break;
104104
}
@@ -119,16 +119,16 @@ public function test_unstable_previous_consent_given($consentType)
119119
serviceId: 'service-provider-entity-id',
120120
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
121121
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
122-
consentType: $consentType,
122+
consentType: $consentType->value,
123123
))
124124
->once()
125125
->andReturn(ConsentVersion::unstable());
126126

127127
switch ($consentType) {
128-
case ConsentType::TYPE_EXPLICIT:
128+
case ConsentType::Explicit:
129129
$this->assertTrue($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
130130
break;
131-
case ConsentType::TYPE_IMPLICIT:
131+
case ConsentType::Implicit:
132132
$this->assertTrue($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
133133
break;
134134
}
@@ -149,16 +149,16 @@ public function test_stable_consent_given($consentType)
149149
serviceId: 'service-provider-entity-id',
150150
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
151151
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
152-
consentType: $consentType,
152+
consentType: $consentType->value,
153153
))
154154
->once()
155155
->andReturn(ConsentVersion::stable());
156156

157157
switch ($consentType) {
158-
case ConsentType::TYPE_EXPLICIT:
158+
case ConsentType::Explicit:
159159
$this->assertTrue($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
160160
break;
161-
case ConsentType::TYPE_IMPLICIT:
161+
case ConsentType::Implicit:
162162
$this->assertTrue($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
163163
break;
164164
}
@@ -184,16 +184,16 @@ public function test_give_consent_toggle_on_stores_only_stable_hash($consentType
184184
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
185185
serviceId: 'service-provider-entity-id',
186186
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
187-
consentType: $consentType,
187+
consentType: $consentType->value,
188188
attributeHash: null,
189189
))
190190
->andReturn(true);
191191

192192
switch ($consentType) {
193-
case ConsentType::TYPE_EXPLICIT:
193+
case ConsentType::Explicit:
194194
$this->assertTrue($this->consent->giveExplicitConsentFor($serviceProvider));
195195
break;
196-
case ConsentType::TYPE_IMPLICIT:
196+
case ConsentType::Implicit:
197197
$this->assertTrue($this->consent->giveImplicitConsentFor($serviceProvider));
198198
break;
199199
}
@@ -219,16 +219,16 @@ public function test_give_consent_toggle_off_stores_both_hashes($consentType)
219219
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
220220
serviceId: 'service-provider-entity-id',
221221
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
222-
consentType: $consentType,
222+
consentType: $consentType->value,
223223
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
224224
))
225225
->andReturn(true);
226226

227227
switch ($consentType) {
228-
case ConsentType::TYPE_EXPLICIT:
228+
case ConsentType::Explicit:
229229
$this->assertTrue($this->consent->giveExplicitConsentFor($serviceProvider));
230230
break;
231-
case ConsentType::TYPE_IMPLICIT:
231+
case ConsentType::Implicit:
232232
$this->assertTrue($this->consent->giveImplicitConsentFor($serviceProvider));
233233
break;
234234
}
@@ -254,7 +254,7 @@ public function test_upgrade_toggle_off_preserves_legacy_hash($consentType)
254254
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
255255
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
256256
serviceId: 'service-provider-entity-id',
257-
consentType: $consentType,
257+
consentType: $consentType->value,
258258
clearLegacyHash: false,
259259
))
260260
->andReturn(true);
@@ -282,7 +282,7 @@ public function test_upgrade_toggle_on_clears_legacy_hash($consentType)
282282
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
283283
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
284284
serviceId: 'service-provider-entity-id',
285-
consentType: $consentType,
285+
consentType: $consentType->value,
286286
clearLegacyHash: true,
287287
))
288288
->andReturn(true);
@@ -354,7 +354,7 @@ public function test_store_consent_hash_sql_resets_deleted_at_on_duplicate(): vo
354354

355355
public static function consentTypeProvider(): iterable
356356
{
357-
yield [ConsentType::TYPE_IMPLICIT];
358-
yield [ConsentType::TYPE_EXPLICIT];
357+
yield [ConsentType::Implicit];
358+
yield [ConsentType::Explicit];
359359
}
360360
}

tests/library/EngineBlock/Test/Corto/Model/ConsentTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function testUpgradeAttributeHashSkippedWhenConsentDisabled()
8080
$this->consentService->shouldNotReceive('retrieveConsentHash');
8181
$this->consentService->shouldNotReceive('updateConsentHash');
8282

83-
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::TYPE_EXPLICIT, ConsentVersion::stable());
84-
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::TYPE_IMPLICIT, ConsentVersion::stable());
83+
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::Explicit, ConsentVersion::stable());
84+
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::Implicit, ConsentVersion::stable());
8585
}
8686

8787
public function testConsentWriteToDatabase()
@@ -175,6 +175,6 @@ public function testNullNameIdReturnsNoConsentWithoutCallingRepository()
175175
$this->assertFalse($consentWithNullUid->giveExplicitConsentFor($serviceProvider));
176176
$this->assertFalse($consentWithNullUid->giveImplicitConsentFor($serviceProvider));
177177
// upgradeAttributeHashFor should not throw when UID is null
178-
$consentWithNullUid->upgradeAttributeHashFor($serviceProvider, ConsentType::TYPE_EXPLICIT, ConsentVersion::notGiven());
178+
$consentWithNullUid->upgradeAttributeHashFor($serviceProvider, ConsentType::Explicit, ConsentVersion::notGiven());
179179
}
180180
}

tests/unit/OpenConext/EngineBlock/Authentication/Dto/ConsentTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function all_values_are_serialized_to_json()
9191
{
9292
$serviceProvider = $this->createServiceProvider();
9393
$consentGivenOn = new DateTime('20080624 10:00:00');
94-
$consentType = ConsentType::explicit();
94+
$consentType = ConsentType::Explicit;
9595

9696
$consent = new Consent(
9797
new ConsentModel(
@@ -135,7 +135,7 @@ public function test_display_name_of_organizations_works_as_intended(
135135
) {
136136
$serviceProvider = $this->createServiceProvider($organizations);
137137
$consentGivenOn = new DateTime('20080624 10:00:00');
138-
$consentType = ConsentType::explicit();
138+
$consentType = ConsentType::Explicit;
139139

140140
$consent = new Consent(
141141
new ConsentModel(
@@ -165,7 +165,7 @@ public function display_name_falls_back_to_name_if_display_name_is_empty()
165165
$serviceProvider->nameNl = 'Name NL';
166166

167167
$consentGivenOn = new DateTime();
168-
$consentType = ConsentType::explicit();
168+
$consentType = ConsentType::Explicit;
169169

170170
$consent = new Consent(
171171
new ConsentModel(
@@ -196,7 +196,7 @@ public function display_name_falls_back_to_entity_id_if_name_is_empty()
196196
$serviceProvider->nameNl = '';
197197

198198
$consentGivenOn = new DateTime();
199-
$consentType = ConsentType::explicit();
199+
$consentType = ConsentType::Explicit;
200200

201201
$consent = new Consent(
202202
new ConsentModel(

0 commit comments

Comments
 (0)