Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TerminNotificationEndpointTrait | |
100.00% |
39 / 39 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| getEntityData | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| updateEntityWithData | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| getEntityById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Termine\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Roles\Role; |
| 6 | use Olz\Entity\Termine\Termin; |
| 7 | use Olz\Entity\Termine\TerminNotification; |
| 8 | use Olz\Entity\Users\User; |
| 9 | use Olz\Utils\WithUtilsTrait; |
| 10 | use PhpTypeScriptApi\HttpError; |
| 11 | |
| 12 | /** |
| 13 | * @phpstan-type OlzTerminNotificationId int |
| 14 | * @phpstan-type OlzTerminNotificationData array{ |
| 15 | * terminId: int, |
| 16 | * firesEarlierSeconds: int, |
| 17 | * title: non-empty-string, |
| 18 | * content: string, |
| 19 | * recipientUserId?: ?int, |
| 20 | * recipientRoleId?: ?int, |
| 21 | * recipientTerminOwnerUser?: ?bool, |
| 22 | * recipientTerminOwnerRole?: ?bool, |
| 23 | * recipientTerminOrganizer?: ?bool, |
| 24 | * recipientTerminVolunteers?: ?bool, |
| 25 | * recipientTerminParticipants?: ?bool, |
| 26 | * } |
| 27 | */ |
| 28 | trait TerminNotificationEndpointTrait { |
| 29 | use WithUtilsTrait; |
| 30 | |
| 31 | /** @return OlzTerminNotificationData */ |
| 32 | public function getEntityData(TerminNotification $entity): array { |
| 33 | return [ |
| 34 | 'terminId' => $entity->getTermin()->getId() ?? 0, |
| 35 | 'firesEarlierSeconds' => $entity->getFiresEarlierSeconds(), |
| 36 | 'title' => $entity->getTitle() ?: '-', |
| 37 | 'content' => $entity->getContent() ?? '', |
| 38 | 'recipientUserId' => $entity->getRecipientUser()?->getId(), |
| 39 | 'recipientRoleId' => $entity->getRecipientRole()?->getId(), |
| 40 | 'recipientTerminOwnerUser' => $entity->getRecipientTerminOwnerUser(), |
| 41 | 'recipientTerminOwnerRole' => $entity->getRecipientTerminOwnerRole(), |
| 42 | 'recipientTerminOrganizer' => $entity->getRecipientTerminOrganizer(), |
| 43 | 'recipientTerminVolunteers' => $entity->getRecipientTerminVolunteers(), |
| 44 | 'recipientTerminParticipants' => $entity->getRecipientTerminParticipants(), |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | /** @param OlzTerminNotificationData $input_data */ |
| 49 | public function updateEntityWithData(TerminNotification $entity, array $input_data): void { |
| 50 | $termin_repo = $this->entityManager()->getRepository(Termin::class); |
| 51 | $termin_id = $input_data['terminId']; |
| 52 | $termin = $termin_repo->findOneBy(['id' => $termin_id]); |
| 53 | $this->generalUtils()->checkNotNull($termin, 'TerminNotification->termin must not be null'); |
| 54 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 55 | $recipient_user_id = $input_data['recipientUserId'] ?? null; |
| 56 | $recipient_user = $user_repo->findOneBy(['id' => $recipient_user_id]); |
| 57 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 58 | $recipient_role_id = $input_data['recipientRoleId'] ?? null; |
| 59 | $recipient_role = $role_repo->findOneBy(['id' => $recipient_role_id]); |
| 60 | |
| 61 | $entity->setTermin($termin); |
| 62 | $entity->setFiresEarlierSeconds($input_data['firesEarlierSeconds']); |
| 63 | $entity->setTitle($input_data['title']); |
| 64 | $entity->setContent($input_data['content']); |
| 65 | $entity->setRecipientUser($recipient_user); |
| 66 | $entity->setRecipientRole($recipient_role); |
| 67 | $entity->setRecipientTerminOwnerUser($input_data['recipientTerminOwnerUser'] ?? false); |
| 68 | $entity->setRecipientTerminOwnerRole($input_data['recipientTerminOwnerRole'] ?? false); |
| 69 | $entity->setRecipientTerminOrganizer($input_data['recipientTerminOrganizer'] ?? false); |
| 70 | $entity->setRecipientTerminVolunteers($input_data['recipientTerminVolunteers'] ?? false); |
| 71 | $entity->setRecipientTerminParticipants($input_data['recipientTerminParticipants'] ?? false); |
| 72 | } |
| 73 | |
| 74 | protected function getEntityById(int $id): TerminNotification { |
| 75 | $repo = $this->entityManager()->getRepository(TerminNotification::class); |
| 76 | $entity = $repo->findOneBy(['id' => $id]); |
| 77 | if (!$entity) { |
| 78 | throw new HttpError(404, "Nicht gefunden."); |
| 79 | } |
| 80 | return $entity; |
| 81 | } |
| 82 | } |