Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.84% |
45 / 49 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RegistrationEndpointTrait | |
91.84% |
45 / 49 |
|
50.00% |
2 / 4 |
25.34 | |
0.00% |
0 / 1 |
| getEntityData | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
4 | |||
| updateEntityWithData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getEntityById | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| getTypeForApi | |
83.33% |
15 / 18 |
|
0.00% |
0 / 1 |
19.50 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Apps\Anmelden\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Anmelden\Registration; |
| 6 | use Olz\Entity\Anmelden\RegistrationInfo; |
| 7 | use Olz\Utils\WithUtilsTrait; |
| 8 | use PhpTypeScriptApi\HttpError; |
| 9 | use PhpTypeScriptApi\PhpStan\IsoDateTime; |
| 10 | |
| 11 | /** |
| 12 | * @phpstan-type OlzRegistrationId non-empty-string |
| 13 | * @phpstan-type OlzRegistrationData array{ |
| 14 | * title: non-empty-string, |
| 15 | * description: string, |
| 16 | * infos: array<OlzRegistrationInfo>, |
| 17 | * opensAt?: ?IsoDateTime, |
| 18 | * closesAt?: ?IsoDateTime, |
| 19 | * } |
| 20 | * @phpstan-type OlzRegistrationInfo array{ |
| 21 | * type: ValidRegistrationInfoType, |
| 22 | * isOptional: bool, |
| 23 | * title: non-empty-string, |
| 24 | * description: string, |
| 25 | * options?: ?( |
| 26 | * array{text: array<non-empty-string>} |
| 27 | * | array{svg: array<non-empty-string>} |
| 28 | * ), |
| 29 | * } |
| 30 | * @phpstan-type ValidRegistrationInfoType 'email'|'firstName'|'lastName'|'gender'|'street'|'postalCode'|'city'|'region'|'countryCode'|'birthdate'|'phone'|'siCardNumber'|'solvNumber'|'string'|'enum'|'reservation' |
| 31 | */ |
| 32 | trait RegistrationEndpointTrait { |
| 33 | use WithUtilsTrait; |
| 34 | |
| 35 | /** @return OlzRegistrationData */ |
| 36 | public function getEntityData(Registration $entity): array { |
| 37 | $infos = []; |
| 38 | $registration_info_repo = $this->entityManager()->getRepository(RegistrationInfo::class); |
| 39 | $registration_infos = $registration_info_repo->findBy( |
| 40 | ['registration' => $entity, 'on_off' => true], |
| 41 | ['indexWithinRegistration' => 'ASC'], |
| 42 | ); |
| 43 | foreach ($registration_infos as $registration_info) { |
| 44 | $options = json_decode($registration_info->getOptions(), true); |
| 45 | $infos[] = [ |
| 46 | 'type' => $this->getTypeForApi($registration_info), |
| 47 | 'isOptional' => $registration_info->getIsOptional(), |
| 48 | 'title' => $registration_info->getTitle() ?: '-', |
| 49 | 'description' => $registration_info->getDescription(), |
| 50 | 'options' => $options, |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | return [ |
| 55 | 'title' => $entity->getTitle() ?: '-', |
| 56 | 'description' => $entity->getDescription(), |
| 57 | 'infos' => $infos, |
| 58 | 'opensAt' => IsoDateTime::fromDateTime($entity->getOpensAt()), |
| 59 | 'closesAt' => IsoDateTime::fromDateTime($entity->getClosesAt()), |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | /** @param OlzRegistrationData $input_data */ |
| 64 | public function updateEntityWithData(Registration $entity, array $input_data): void { |
| 65 | $entity->setTitle($input_data['title']); |
| 66 | $entity->setDescription($input_data['description']); |
| 67 | $entity->setOpensAt($input_data['opensAt'] ?? null); |
| 68 | $entity->setClosesAt($input_data['closesAt'] ?? null); |
| 69 | } |
| 70 | |
| 71 | protected function getEntityById(int $id): Registration { |
| 72 | $repo = $this->entityManager()->getRepository(Registration::class); |
| 73 | $entity = $repo->findOneBy(['id' => $id]); |
| 74 | if (!$entity) { |
| 75 | throw new HttpError(404, "Nicht gefunden."); |
| 76 | } |
| 77 | return $entity; |
| 78 | } |
| 79 | |
| 80 | // --- |
| 81 | |
| 82 | /** @return ValidRegistrationInfoType */ |
| 83 | protected function getTypeForApi(RegistrationInfo $entity): string { |
| 84 | switch ($entity->getType()) { |
| 85 | case 'email': return 'email'; |
| 86 | case 'firstName': return 'firstName'; |
| 87 | case 'lastName': return 'lastName'; |
| 88 | case 'gender': return 'gender'; |
| 89 | case 'street': return 'street'; |
| 90 | case 'postalCode': return 'postalCode'; |
| 91 | case 'city': return 'city'; |
| 92 | case 'region': return 'region'; |
| 93 | case 'countryCode': return 'countryCode'; |
| 94 | case 'birthdate': return 'birthdate'; |
| 95 | case 'phone': return 'phone'; |
| 96 | case 'siCardNumber': return 'siCardNumber'; |
| 97 | case 'solvNumber': return 'solvNumber'; |
| 98 | case 'string': return 'string'; |
| 99 | case 'enum': return 'enum'; |
| 100 | case 'reservation': return 'reservation'; |
| 101 | default: throw new \Exception("Unknown registration info type: {$entity->getType()} ({$entity})"); |
| 102 | } |
| 103 | } |
| 104 | } |