GetQrCodeCommandHandler.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\QrCode; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 13 | use AmeliaBooking\Application\Services\Booking\EventApplicationService; |
| 14 | use AmeliaBooking\Application\Services\QrCode\QrCodeApplicationService; |
| 15 | use AmeliaBooking\Domain\Collection\Collection; |
| 16 | use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException; |
| 17 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 18 | use AmeliaBooking\Domain\Entity\Booking\Appointment\CustomerBooking; |
| 19 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 20 | use AmeliaBooking\Domain\Entity\Entities; |
| 21 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 22 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 23 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 24 | use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository; |
| 25 | use Interop\Container\Exception\ContainerException; |
| 26 | use Slim\Exception\ContainerValueNotFoundException; |
| 27 | |
| 28 | /** |
| 29 | * Class GetQrCodeCommandHandler |
| 30 | * |
| 31 | * @package AmeliaBooking\Application\Commands\QrCode |
| 32 | */ |
| 33 | class GetQrCodeCommandHandler extends CommandHandler |
| 34 | { |
| 35 | /** |
| 36 | * @param GetQrCodeCommand $command |
| 37 | * |
| 38 | * @return CommandResult |
| 39 | * @throws InvalidArgumentException |
| 40 | * @throws QueryExecutionException |
| 41 | * @throws ContainerException |
| 42 | * @throws ContainerValueNotFoundException |
| 43 | * @throws NotFoundException|AccessDeniedException |
| 44 | */ |
| 45 | |
| 46 | public function handle(GetQrCodeCommand $command) |
| 47 | { |
| 48 | $result = new CommandResult(); |
| 49 | |
| 50 | try { |
| 51 | /** @var AbstractUser $user */ |
| 52 | $user = $command->getUserApplicationService()->authorization( |
| 53 | $command->getToken(), |
| 54 | Entities::CUSTOMER |
| 55 | ); |
| 56 | } catch (AuthorizationException $e) { |
| 57 | $result->setResult(CommandResult::RESULT_ERROR); |
| 58 | $result->setData( |
| 59 | [ |
| 60 | 'reauthorize' => true |
| 61 | ] |
| 62 | ); |
| 63 | |
| 64 | return $result; |
| 65 | } |
| 66 | |
| 67 | /** @var EventApplicationService $eventApplicationService */ |
| 68 | $eventApplicationService = $this->container->get('application.booking.event.service'); |
| 69 | |
| 70 | /** @var EventRepository $eventRepository */ |
| 71 | $eventRepository = $this->container->get('domain.booking.event.repository'); |
| 72 | |
| 73 | $params = $command->getField('params'); |
| 74 | $eventId = (int)($params['eventId'] ?? null); |
| 75 | $bookingId = (int)($params['bookingId'] ?? null); |
| 76 | $ticketManualCode = (string)($params['ticketManualCode'] ?? ''); |
| 77 | |
| 78 | $eventsBookings = $eventRepository->getBookingsByCriteria([ |
| 79 | 'customerId' => $user->getId()->getValue(), |
| 80 | 'customerBookingId' => $bookingId, |
| 81 | 'fetchBookingsTickets' => true, |
| 82 | 'fetchBookingsPayments' => true, |
| 83 | ]); |
| 84 | |
| 85 | /** @var CustomerBooking $booking */ |
| 86 | $booking = $eventsBookings->getItem($eventId)->getItem($bookingId); |
| 87 | |
| 88 | if (!$booking || $booking->getCustomerId()->getValue() !== $user->getId()->getValue()) { |
| 89 | $result->setResult(CommandResult::RESULT_ERROR); |
| 90 | $result->setMessage('Access denied'); |
| 91 | return $result; |
| 92 | } |
| 93 | |
| 94 | $hasPaid = false; |
| 95 | $hasOnSite = false; |
| 96 | |
| 97 | foreach ($booking->getPayments()->getItems() as $payment) { |
| 98 | if (!$hasPaid && $payment->getStatus()->getValue() === 'paid') { |
| 99 | $hasPaid = true; |
| 100 | } |
| 101 | if (!$hasOnSite && $payment->getGateway()->getName()->getValue() === 'onSite') { |
| 102 | $hasOnSite = true; |
| 103 | } |
| 104 | if ($hasPaid && $hasOnSite) { |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (!$hasPaid && !$hasOnSite) { |
| 110 | $result->setResult(CommandResult::RESULT_ERROR); |
| 111 | $result->setMessage('You have to pay full amount in order to generate E-Tickets'); |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | |
| 116 | /** @var Event $event */ |
| 117 | $event = $eventApplicationService->getEventById( |
| 118 | $eventId, |
| 119 | [ |
| 120 | 'fetchEventsPeriods' => true, |
| 121 | 'fetchEventsTickets' => true, |
| 122 | ] |
| 123 | ); |
| 124 | |
| 125 | $qrFile = null; |
| 126 | |
| 127 | if (empty($booking->getQrCodes()->getValue())) { |
| 128 | $result->setResult(CommandResult::RESULT_ERROR); |
| 129 | $result->setMessage('No E-Ticktes found for this booking'); |
| 130 | |
| 131 | return $result; |
| 132 | } else { |
| 133 | /** @var QrCodeApplicationService $qrApplicationService */ |
| 134 | $qrApplicationService = $this->container->get('application.qrcode.service'); |
| 135 | $qrFile = $qrApplicationService->createQrCodeEventTickets( |
| 136 | $event->toArray(), |
| 137 | $booking->toArray(), |
| 138 | $ticketManualCode |
| 139 | ); |
| 140 | |
| 141 | if (empty($qrFile)) { |
| 142 | $result->setResult(CommandResult::RESULT_ERROR); |
| 143 | $result->setMessage('No E-Ticktes found for this booking'); |
| 144 | |
| 145 | return $result; |
| 146 | } |
| 147 | |
| 148 | $qrFile['content'] = base64_encode($qrFile['content']); |
| 149 | $qrFile['mime'] = $qrFile['type']; |
| 150 | $qrFile['encoding'] = 'base64'; |
| 151 | } |
| 152 | |
| 153 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 154 | $result->setAttachment(true); |
| 155 | $result->setFile($qrFile); |
| 156 | $result->setMessage('Qr code generated successfully'); |
| 157 | |
| 158 | return $result; |
| 159 | } |
| 160 | } |
| 161 |