Files
myep/src/Form/Service/Condition/AuthenticatedUserPersonalDataCondition.php
T

88 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that determines if personal data fields should be shown as static text.
*
* This condition protects the logged-in user's personal data from being modified
* within the booking form. Instead, users should edit their data via the personal
* data form to prevent:
* - Creating duplicate customer records in BPN
* - Disconnecting bookings from the user's account
* - Causing data inconsistencies between booking and account data
*
* The condition compares the participant's personId with the booking applicant's
* personId to determine if the participant IS the logged-in user:
* - Customer-initiated bookings: Participant 0's personId matches applicant's personId
* - Agency-initiated bookings: Participant 0's personId differs from applicant's personId
*
* In CREATE mode: Checks if participant has a personId (prepopulated from BPN account)
* In EDIT mode: Compares participant's personId with applicant's personId
*/
class AuthenticatedUserPersonalDataCondition implements FieldConditionInterface
{
/**
* Evaluates if personal data fields should be shown as static text.
*
* In CREATE mode: Returns true when participant has a personId (prepopulated).
* In EDIT mode: Returns true when participant's personId matches applicant's personId,
* indicating the participant IS the logged-in user.
*
* @param BookingDto $bookingDto The booking data
* @param int $participantIndex The index of the participant being evaluated
* @param array<string, mixed> $formData Current form data (unused)
*
* @return bool True if fields should be shown as static text
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
$participant = $bookingDto->getParticipant($participantIndex);
if (null === $participant) {
return false;
}
// In edit mode, compare participant's personId with applicant's personId
// This distinguishes customer-initiated (match) from agency-initiated (no match) bookings
if (BookingDto::MODE_EDIT === $bookingDto->getMode()) {
$applicantPersonId = $bookingDto->booking?->applicant?->personId;
if (null === $applicantPersonId) {
return false;
}
return $participant->personId === $applicantPersonId;
}
// In create mode, check if participant has personId (prepopulated from BPN account)
return null !== $participant->personId;
}
/**
* Returns field names that this condition depends on.
*
* This condition is based on personId which is set during prepopulation
* and doesn't change during form interaction, so no field dependencies.
*
* @return string[] Empty array - no field dependencies
*/
public function getDependentFields(): array
{
return [];
}
/**
* Returns a human-readable description of this condition.
*
* @return string Description of the authenticated user personal data protection logic
*/
public function getDescription(): string
{
return 'Personal data is shown as static text for the logged-in user (edit via personal data form)';
}
}