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