ScanEventTicketCommandHandler.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Mobile\Events; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandResult; |
| 6 | use AmeliaBooking\Application\Commands\QrCode\ScanQrCodeCommand; |
| 7 | use AmeliaBooking\Application\Commands\QrCode\ScanQrCodeCommandHandler; |
| 8 | |
| 9 | /** |
| 10 | * Mobile-API wrapper around ScanQrCodeCommandHandler. |
| 11 | * |
| 12 | * Key differences from the web scanner: |
| 13 | * - scannedAt is generated server-side (current date); the client does not send it. |
| 14 | * - "Already scanned" cases are returned as HTTP 200 with alreadyScanned:true |
| 15 | * instead of HTTP 409, so the mobile app can show a distinct UI state. |
| 16 | */ |
| 17 | class ScanEventTicketCommandHandler extends ScanQrCodeCommandHandler |
| 18 | { |
| 19 | public $mandatoryFields = ['ticketManualCode']; |
| 20 | |
| 21 | private const ALREADY_SCANNED_CODES = [ |
| 22 | 'ticket_already_scanned', |
| 23 | 'group_ticket_already_scanned', |
| 24 | 'all_already_scanned', |
| 25 | ]; |
| 26 | |
| 27 | public function handle(ScanQrCodeCommand $command) |
| 28 | { |
| 29 | // Generate the scan date server-side so the client cannot forge it. |
| 30 | $command->setField('scannedAt', date('Y-m-d')); |
| 31 | |
| 32 | $result = parent::handle($command); |
| 33 | |
| 34 | if ($result->getResult() === CommandResult::RESULT_ERROR) { |
| 35 | $data = $result->getData(); |
| 36 | $message = isset($data['message']) ? (string) $data['message'] : ''; |
| 37 | |
| 38 | if (in_array($message, self::ALREADY_SCANNED_CODES, true)) { |
| 39 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 40 | $result->setData(['alreadyScanned' => true]); |
| 41 | } |
| 42 | |
| 43 | return $result; |
| 44 | } |
| 45 | |
| 46 | $result->setData(['alreadyScanned' => false]); |
| 47 | |
| 48 | return $result; |
| 49 | } |
| 50 | } |
| 51 |