Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateRegistrationEndpoint
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 configure
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace Olz\Apps\Anmelden\Endpoints;
4
5use Olz\Api\OlzCreateEntityTypedEndpoint;
6use Olz\Entity\Anmelden\Registration;
7use Olz\Entity\Anmelden\RegistrationInfo;
8
9/**
10 * @phpstan-import-type OlzRegistrationId from RegistrationEndpointTrait
11 * @phpstan-import-type OlzRegistrationData from RegistrationEndpointTrait
12 *
13 * TODO: Those should not be necessary!
14 * @phpstan-import-type OlzRegistrationInfo from RegistrationEndpointTrait
15 * @phpstan-import-type ValidRegistrationInfoType from RegistrationEndpointTrait
16 *
17 * @extends OlzCreateEntityTypedEndpoint<OlzRegistrationId, OlzRegistrationData>
18 */
19class CreateRegistrationEndpoint extends OlzCreateEntityTypedEndpoint {
20    use RegistrationEndpointTrait;
21
22    public function configure(): void {
23        parent::configure();
24        $this->configureRegistrationEndpointTrait();
25        $this->phpStanUtils->registerTypeImport(RegistrationEndpointTrait::class);
26    }
27
28    protected function handle(mixed $input): mixed {
29        $this->checkPermission('any');
30
31        $input_data = $input['data'];
32
33        $registration = new Registration();
34        $this->entityUtils()->createOlzEntity($registration, $input['meta']);
35        $this->updateEntityWithData($registration, $input['data']);
36
37        $this->entityManager()->persist($registration);
38
39        foreach ($input_data['infos'] as $index => $info_spec) {
40            $title_ident = preg_replace('/[^a-zA-Z0-9]+/', '_', $info_spec['title']);
41            $ident = "{$index}-{$title_ident}";
42
43            $options_json = json_encode($info_spec['options'] ?? []) ?: '{}';
44
45            $registration_info = new RegistrationInfo();
46            $this->entityUtils()->createOlzEntity($registration_info, $input['meta']);
47            $registration_info->setRegistration($registration);
48            $registration_info->setIndexWithinRegistration($index);
49            $registration_info->setIdent($ident);
50            $registration_info->setTitle($info_spec['title']);
51            $registration_info->setDescription($info_spec['description']);
52            $registration_info->setType($info_spec['type']);
53            $registration_info->setIsOptional($info_spec['isOptional'] ? true : false);
54            $registration_info->setOptions($options_json);
55
56            $this->entityManager()->persist($registration_info);
57        }
58        $this->entityManager()->flush();
59
60        $internal_id = $registration->getId() ?? 0;
61        $external_id = $this->idUtils()->toExternalId($internal_id, 'Registration') ?: '-';
62
63        return [
64            'id' => $external_id,
65        ];
66    }
67}