Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| UserEndpointTrait | |
0.00% |
0 / 71 |
|
0.00% |
0 / 7 |
1190 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
240 | |||
| updateEntityWithData | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
20 | |||
| persistUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| editUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getGenderForApi | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
42 | |||
| getSiCardNumberForApi | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Users\Endpoints; |
| 4 | |
| 5 | use Olz\Api\ApiObjects\IsoCountry; |
| 6 | use Olz\Entity\Users\User; |
| 7 | use Olz\Utils\WithUtilsTrait; |
| 8 | use PhpTypeScriptApi\HttpError; |
| 9 | use PhpTypeScriptApi\PhpStan\IsoDate; |
| 10 | |
| 11 | /** |
| 12 | * @phpstan-type OlzUserId int |
| 13 | * @phpstan-type OlzUserData array{ |
| 14 | * parentUserId?: ?int, |
| 15 | * firstName: non-empty-string, |
| 16 | * lastName: non-empty-string, |
| 17 | * username: non-empty-string, |
| 18 | * password?: ?non-empty-string, |
| 19 | * email?: ?non-empty-string, |
| 20 | * phone?: ?non-empty-string, |
| 21 | * gender?: ?('M'|'F'|'O'), |
| 22 | * birthdate?: ?IsoDate, |
| 23 | * street?: ?non-empty-string, |
| 24 | * postalCode?: ?non-empty-string, |
| 25 | * city?: ?non-empty-string, |
| 26 | * region?: ?non-empty-string, |
| 27 | * countryCode?: ?IsoCountry, |
| 28 | * siCardNumber?: ?int<100000, max>, |
| 29 | * solvNumber?: ?non-empty-string, |
| 30 | * ahvNumber?: ?non-empty-string, |
| 31 | * dressSize?: ?non-empty-string, |
| 32 | * avatarImageId?: ?non-empty-string, |
| 33 | * } |
| 34 | */ |
| 35 | trait UserEndpointTrait { |
| 36 | use WithUtilsTrait; |
| 37 | |
| 38 | /** @return OlzUserData */ |
| 39 | public function getEntityData(User $entity): array { |
| 40 | return [ |
| 41 | 'parentUserId' => $entity->getParentUserId(), |
| 42 | 'firstName' => $entity->getFirstName() ?: '-', |
| 43 | 'lastName' => $entity->getLastName() ?: '-', |
| 44 | 'username' => $entity->getUsername() ?: '-', |
| 45 | 'password' => null, |
| 46 | 'email' => $entity->getEmail() ? $entity->getEmail() : null, |
| 47 | 'phone' => $entity->getPhone() ? $entity->getPhone() : null, |
| 48 | 'gender' => $this->getGenderForApi($entity), |
| 49 | 'birthdate' => IsoDate::fromDateTime($entity->getBirthdate()), |
| 50 | 'street' => $entity->getStreet() ? $entity->getStreet() : null, |
| 51 | 'postalCode' => $entity->getPostalCode() ? $entity->getPostalCode() : null, |
| 52 | 'city' => $entity->getCity() ? $entity->getCity() : null, |
| 53 | 'region' => $entity->getRegion() ? $entity->getRegion() : null, |
| 54 | 'countryCode' => $entity->getCountryCode() ? IsoCountry::fromData($entity->getCountryCode()) : null, |
| 55 | 'siCardNumber' => $this->getSiCardNumberForApi($entity), |
| 56 | 'solvNumber' => $entity->getSolvNumber() ? $entity->getSolvNumber() : null, |
| 57 | 'ahvNumber' => $entity->getAhvNumber() ? $entity->getAhvNumber() : null, |
| 58 | 'dressSize' => $entity->getDressSize() ? $entity->getDressSize() : null, |
| 59 | 'avatarImageId' => $entity->getAvatarImageId() ? $entity->getAvatarImageId() : null, |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | /** @param OlzUserData $input_data */ |
| 64 | public function updateEntityWithData(User $entity, array $input_data): void { |
| 65 | $birthdate = $input_data['birthdate'] ?? null; |
| 66 | $valid_birthdate = $birthdate |
| 67 | ? new \DateTime($birthdate->format('Y-m-d').' 12:00:00') |
| 68 | : null; |
| 69 | $avatar_image_id = $input_data['avatarImageId'] ?? null; |
| 70 | $valid_avatar_image_id = $avatar_image_id |
| 71 | ? $this->uploadUtils()->getValidUploadId($avatar_image_id) |
| 72 | : null; |
| 73 | $si_card_number = $input_data['siCardNumber'] ?? null; |
| 74 | $valid_si_card_number = $si_card_number ? strval($si_card_number) : null; |
| 75 | |
| 76 | $entity->setParentUserId($input_data['parentUserId'] ?? null); |
| 77 | $entity->setUsername($input_data['username']); |
| 78 | $entity->setFirstName($input_data['firstName']); |
| 79 | $entity->setLastName($input_data['lastName']); |
| 80 | $entity->setEmail($input_data['email'] ?? null); |
| 81 | $entity->setPhone($input_data['phone'] ?? null); |
| 82 | $entity->setGender($input_data['gender'] ?? null); |
| 83 | $entity->setBirthdate($valid_birthdate); |
| 84 | $entity->setStreet($input_data['street'] ?? null); |
| 85 | $entity->setPostalCode($input_data['postalCode'] ?? null); |
| 86 | $entity->setCity($input_data['city'] ?? null); |
| 87 | $entity->setRegion($input_data['region'] ?? null); |
| 88 | $entity->setCountryCode($input_data['countryCode']?->data()); |
| 89 | $entity->setSiCardNumber($valid_si_card_number); |
| 90 | $entity->setSolvNumber($input_data['solvNumber'] ?? null); |
| 91 | $entity->setAhvNumber($input_data['ahvNumber'] ?? null); |
| 92 | $entity->setDressSize($input_data['dressSize'] ?? null); |
| 93 | $entity->setAvatarImageId($valid_avatar_image_id); |
| 94 | } |
| 95 | |
| 96 | /** @param OlzUserData $input_data */ |
| 97 | public function persistUploads(User $entity, array $input_data): void { |
| 98 | if ($entity->getAvatarImageId()) { |
| 99 | $this->persistOlzImages($entity, [$entity->getAvatarImageId()]); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public function editUploads(User $entity): void { |
| 104 | if ($entity->getAvatarImageId() !== null) { |
| 105 | $this->editOlzImages($entity, [$entity->getAvatarImageId()]); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | protected function getEntityById(int $id): User { |
| 110 | $repo = $this->entityManager()->getRepository(User::class); |
| 111 | $entity = $repo->findOneBy(['id' => $id]); |
| 112 | if (!$entity) { |
| 113 | throw new HttpError(404, "Nicht gefunden."); |
| 114 | } |
| 115 | return $entity; |
| 116 | } |
| 117 | |
| 118 | // --- |
| 119 | |
| 120 | /** @return 'M'|'F'|'O'|null */ |
| 121 | protected function getGenderForApi(User $entity): ?string { |
| 122 | switch ($entity->getGender()) { |
| 123 | case 'M': return 'M'; |
| 124 | case 'F': return 'F'; |
| 125 | case 'O': return 'O'; |
| 126 | case null: return null; |
| 127 | default: throw new \Exception("Unknown Gender: {$entity->getGender()} ({$entity})"); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** @return ?int<100000, max> */ |
| 132 | protected function getSiCardNumberForApi(User $entity): ?int { |
| 133 | $string = $entity->getSiCardNumber(); |
| 134 | if (!$string) { |
| 135 | return null; |
| 136 | } |
| 137 | $number = intval($string); |
| 138 | if ($number < 100000) { |
| 139 | throw new \Exception("Invalid SI Card Number: {$string} ({$entity})"); |
| 140 | } |
| 141 | return $number; |
| 142 | } |
| 143 | } |