Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| BookingEndpointTrait | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| updateEntityWithData | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Apps\Anmelden\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Anmelden\Booking; |
| 6 | use Olz\Entity\Anmelden\Registration; |
| 7 | use Olz\Entity\Anmelden\RegistrationInfo; |
| 8 | use Olz\Utils\WithUtilsTrait; |
| 9 | use PhpTypeScriptApi\HttpError; |
| 10 | |
| 11 | /** |
| 12 | * @phpstan-type OlzBookingId non-empty-string |
| 13 | * @phpstan-type OlzBookingData array{ |
| 14 | * registrationId: non-empty-string, |
| 15 | * values: array<string, mixed>, |
| 16 | * } |
| 17 | */ |
| 18 | trait BookingEndpointTrait { |
| 19 | use WithUtilsTrait; |
| 20 | |
| 21 | /** @return OlzBookingData */ |
| 22 | public function getEntityData(Booking $entity): array { |
| 23 | $registration = $entity->getRegistration(); |
| 24 | $registration_id = $registration->getId() ?? 0; |
| 25 | $external_registration_id = $this->idUtils()->toExternalId($registration_id, 'Registration') ?: '-'; |
| 26 | |
| 27 | $values_json = json_decode($entity->getFormData(), true); |
| 28 | $valid_values = []; |
| 29 | $registration_info_repo = $this->entityManager()->getRepository(RegistrationInfo::class); |
| 30 | foreach ($values_json as $ident => $value) { |
| 31 | $registration_info = $registration_info_repo->findOneBy([ |
| 32 | 'registration' => $registration, |
| 33 | 'ident' => $ident, |
| 34 | ]); |
| 35 | if (!$registration_info) { |
| 36 | $this->log()->warning("Creating booking with unknown info '{$ident}' for registration {$registration->getId()}."); |
| 37 | } |
| 38 | // TODO: Validate reservation is not duplicate |
| 39 | $valid_values[$ident] = $value; |
| 40 | } |
| 41 | return [ |
| 42 | 'registrationId' => $external_registration_id, |
| 43 | 'values' => $valid_values, |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | /** @param OlzBookingData $input_data */ |
| 48 | public function updateEntityWithData(Booking $entity, array $input_data): void { |
| 49 | $current_user = $this->authUtils()->getCurrentUser(); |
| 50 | |
| 51 | $external_registration_id = $input_data['registrationId']; |
| 52 | $internal_registration_id = $this->idUtils()->toInternalId($external_registration_id, 'Registration'); |
| 53 | $registration_repo = $this->entityManager()->getRepository(Registration::class); |
| 54 | $registration = $registration_repo->findOneBy(['id' => $internal_registration_id]); |
| 55 | |
| 56 | if (!$registration) { |
| 57 | throw new HttpError(400, "Invalid registration: {$external_registration_id}"); |
| 58 | } |
| 59 | |
| 60 | $valid_values = []; |
| 61 | $registration_info_repo = $this->entityManager()->getRepository(RegistrationInfo::class); |
| 62 | foreach ($input_data['values'] as $ident => $value) { |
| 63 | $registration_info = $registration_info_repo->findOneBy([ |
| 64 | 'registration' => $registration, |
| 65 | 'ident' => $ident, |
| 66 | ]); |
| 67 | if (!$registration_info) { |
| 68 | $this->log()->warning("Creating booking with unknown info '{$ident}' for registration {$internal_registration_id}."); |
| 69 | } |
| 70 | // TODO: Validate reservation is not duplicate |
| 71 | $valid_values[$ident] = $value; |
| 72 | } |
| 73 | $values_json = json_encode($valid_values) ?: '{}'; |
| 74 | |
| 75 | $entity->setRegistration($registration); |
| 76 | $entity->setUser($current_user); |
| 77 | $entity->setFormData($values_json); |
| 78 | } |
| 79 | } |