Skip to content

refactor: convert ConsentType to a PHP 8.1 backed enum#1929

Open
kayjoosten wants to merge 4 commits intofeature/stabilize-consent-hashfrom
feature/consent-type-enum
Open

refactor: convert ConsentType to a PHP 8.1 backed enum#1929
kayjoosten wants to merge 4 commits intofeature/stabilize-consent-hashfrom
feature/consent-type-enum

Conversation

@kayjoosten
Copy link
Contributor

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

When the consent feature toggle is off, upgradeAttributeHashFor was
still calling _hasStoredConsent, triggering unnecessary DB queries and
hash calculations. Added the same _consentEnabled early-return guard
that already exists on explicitConsentWasGivenFor and
implicitConsentWasGivenFor.
Added a dedicated test (testUpgradeAttributeHashSkippedWhenConsentDisabled)
that asserts retrieveConsentHash and updateConsentHash are never called
when consent is disabled.
C1: ConsentService::countAllFor used findAllFor()+count() which loaded
all consent rows into memory just to count them. Now delegates directly
to consentRepository->countTotalConsent() which issues a COUNT(*) query.
C2: Consent/Factory.php type-hinted the concrete ConsentHashService
class instead of ConsentHashServiceInterface, violating the Dependency
Inversion Principle. Changed constructor parameter, property docblock,
and use import to reference the interface.
_hasStoredConsent() and _updateConsent() called sha1() on the result of
_getConsentUid() without a null guard. _storeConsent() already had this
guard. Added consistent guards to the remaining two methods:
- _hasStoredConsent() returns ConsentVersion::notGiven() when uid is null
- _updateConsent() returns false when uid is null

Also fixed the ConsentTest:
- Mock getNameIdValue() to return a real user ID so tests exercise the
  actual consent flow instead of hitting the null path
- testConsentDisabledDoesNotWriteToDatabase: use shouldNotReceive() for
  store/retrieve/update to assert the service is never called, and add
  assertTrue() assertions on return values
- testConsentWriteToDatabase: consolidate mock setup, add assertTrue()
  assertions so the test is no longer marked risky by PHPUnit
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
@kayjoosten
Copy link
Contributor Author

Note: @johanib ik zag hier een @deprecated van 2015.... dus ik kon het echt niet laten. Ik heb het wel apart getrokken van de ander branch maar als je dit kan reviewen merge ik hem in de andere pr.

@kayjoosten kayjoosten requested a review from johanib March 6, 2026 14:36
@kayjoosten kayjoosten force-pushed the feature/stabilize-consent-hash branch 6 times, most recently from b183015 to c3faaec Compare March 9, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant