Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
46 / 50 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
RegistrationEndpointTrait | |
92.00% |
46 / 50 |
|
60.00% |
3 / 5 |
26.35 | |
0.00% |
0 / 1 |
configureRegistrationEndpointTrait | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
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 | public function configureRegistrationEndpointTrait(): void { |
36 | $this->phpStanUtils->registerApiObject(IsoDateTime::class); |
37 | } |
38 | |
39 | /** @return OlzRegistrationData */ |
40 | public function getEntityData(Registration $entity): array { |
41 | $infos = []; |
42 | $registration_info_repo = $this->entityManager()->getRepository(RegistrationInfo::class); |
43 | $registration_infos = $registration_info_repo->findBy( |
44 | ['registration' => $entity, 'on_off' => true], |
45 | ['indexWithinRegistration' => 'ASC'], |
46 | ); |
47 | foreach ($registration_infos as $registration_info) { |
48 | $options = json_decode($registration_info->getOptions(), true); |
49 | $infos[] = [ |
50 | 'type' => $this->getTypeForApi($registration_info), |
51 | 'isOptional' => $registration_info->getIsOptional(), |
52 | 'title' => $registration_info->getTitle() ?: '-', |
53 | 'description' => $registration_info->getDescription(), |
54 | 'options' => $options, |
55 | ]; |
56 | } |
57 | |
58 | return [ |
59 | 'title' => $entity->getTitle() ?: '-', |
60 | 'description' => $entity->getDescription(), |
61 | 'infos' => $infos, |
62 | 'opensAt' => IsoDateTime::fromDateTime($entity->getOpensAt()), |
63 | 'closesAt' => IsoDateTime::fromDateTime($entity->getClosesAt()), |
64 | ]; |
65 | } |
66 | |
67 | /** @param OlzRegistrationData $input_data */ |
68 | public function updateEntityWithData(Registration $entity, array $input_data): void { |
69 | $entity->setTitle($input_data['title']); |
70 | $entity->setDescription($input_data['description']); |
71 | $entity->setOpensAt($input_data['opensAt'] ?? null); |
72 | $entity->setClosesAt($input_data['closesAt'] ?? null); |
73 | } |
74 | |
75 | protected function getEntityById(int $id): Registration { |
76 | $repo = $this->entityManager()->getRepository(Registration::class); |
77 | $entity = $repo->findOneBy(['id' => $id]); |
78 | if (!$entity) { |
79 | throw new HttpError(404, "Nicht gefunden."); |
80 | } |
81 | return $entity; |
82 | } |
83 | |
84 | // --- |
85 | |
86 | /** @return ValidRegistrationInfoType */ |
87 | protected function getTypeForApi(RegistrationInfo $entity): string { |
88 | switch ($entity->getType()) { |
89 | case 'email': return 'email'; |
90 | case 'firstName': return 'firstName'; |
91 | case 'lastName': return 'lastName'; |
92 | case 'gender': return 'gender'; |
93 | case 'street': return 'street'; |
94 | case 'postalCode': return 'postalCode'; |
95 | case 'city': return 'city'; |
96 | case 'region': return 'region'; |
97 | case 'countryCode': return 'countryCode'; |
98 | case 'birthdate': return 'birthdate'; |
99 | case 'phone': return 'phone'; |
100 | case 'siCardNumber': return 'siCardNumber'; |
101 | case 'solvNumber': return 'solvNumber'; |
102 | case 'string': return 'string'; |
103 | case 'enum': return 'enum'; |
104 | case 'reservation': return 'reservation'; |
105 | default: throw new \Exception("Unknown registration info type: {$entity->getType()} ({$entity})"); |
106 | } |
107 | } |
108 | } |