AbstractQrCodeInfrastructureService.php
2 months ago
QrCodeInfrastructureService.php
2 months ago
StarterQrCodeInfrastructureService.php
2 months ago
QrCodeInfrastructureService.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Services\QrCode; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 6 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 7 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 8 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 9 | use AmeliaVendor\Dompdf\Dompdf; |
| 10 | use BaconQrCode\Renderer\ImageRenderer; |
| 11 | use BaconQrCode\Renderer\Image\SvgImageBackEnd; |
| 12 | use BaconQrCode\Renderer\RendererStyle\RendererStyle; |
| 13 | use BaconQrCode\Writer; |
| 14 | |
| 15 | /** |
| 16 | * Class QrCodeInfrastructureService |
| 17 | * |
| 18 | * @package AmeliaBooking\Infrastructure\Services\QrCode |
| 19 | */ |
| 20 | class QrCodeInfrastructureService extends AbstractQrCodeInfrastructureService |
| 21 | { |
| 22 | /** |
| 23 | * @throws InvalidArgumentException |
| 24 | * @throws NotFoundException |
| 25 | * @throws QueryExecutionException |
| 26 | */ |
| 27 | public function generateQrCode(array $qrData): array |
| 28 | { |
| 29 | $eventStart = strtotime($qrData['eventStartDateTime']); |
| 30 | |
| 31 | if (!$eventStart) { |
| 32 | throw new InvalidArgumentException('Invalid eventStartDateTime provided.'); |
| 33 | } |
| 34 | |
| 35 | /** @var SettingsService $settingsService */ |
| 36 | $settingsService = $this->container->get('domain.settings.service'); |
| 37 | |
| 38 | $dateFormat = $settingsService->getSetting('wordpress', 'dateFormat'); |
| 39 | $timeFormat = $settingsService->getSetting('wordpress', 'timeFormat'); |
| 40 | |
| 41 | $renderer = new ImageRenderer( |
| 42 | new RendererStyle(300, 2), |
| 43 | new SvgImageBackEnd() |
| 44 | ); |
| 45 | |
| 46 | $qrDataItem = [ |
| 47 | 'type' => $qrData['type'], |
| 48 | 'data' => base64_encode((new Writer($renderer))->writeString($qrData['qrCodeData'])), |
| 49 | 'mimeType' => 'image/svg+xml', |
| 50 | 'ticketManualCode' => $qrData['ticketManualCode'], |
| 51 | 'bookingId' => $qrData['bookingId'], |
| 52 | 'eventName' => $qrData['eventName'], |
| 53 | 'eventStartDateTime' => date_i18n( |
| 54 | $dateFormat . ' ' . $timeFormat, |
| 55 | $eventStart |
| 56 | ), |
| 57 | ]; |
| 58 | |
| 59 | if (array_key_exists('eventLocation', $qrData)) { |
| 60 | $qrDataItem['eventLocation'] = $qrData['eventLocation']; |
| 61 | } |
| 62 | |
| 63 | if (array_key_exists('eventTicketName', $qrData)) { |
| 64 | $qrDataItem['eventTicketName'] = $qrData['eventTicketName']; |
| 65 | } |
| 66 | |
| 67 | ob_start(); |
| 68 | include AMELIA_PATH . '/templates/qrCode/qrCode.inc'; |
| 69 | $html = ob_get_clean(); |
| 70 | |
| 71 | $dompdf = new Dompdf(); |
| 72 | |
| 73 | $dompdf->setPaper('A4'); |
| 74 | |
| 75 | $dompdf->loadHtml($html); |
| 76 | |
| 77 | $dompdf->render(); |
| 78 | |
| 79 | $ticketPdfName = ($qrDataItem['type'] === 'ticket' |
| 80 | ? 'Single Ticket' : 'Group Ticket') . (array_key_exists('eventTicketName', $qrDataItem) ? '-' . $qrDataItem['eventTicketName'] |
| 81 | : '') . ' - ' . $qrDataItem['eventName'] . '.pdf'; |
| 82 | |
| 83 | return [ |
| 84 | 'name' => $ticketPdfName, |
| 85 | 'type' => 'application/pdf', |
| 86 | 'content' => $dompdf->output(), |
| 87 | ]; |
| 88 | } |
| 89 | } |
| 90 |