src/EventSubscriber/OrderPlacedSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\OrderPlacedEvent;
  4. use App\Service\SmsProClient;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class OrderPlacedSubscriber implements EventSubscriberInterface
  8. {
  9.     private SmsProClient $smsProClient;
  10.     private LoggerInterface $logger;
  11.     public function __construct(SmsProClient $smsProClient, LoggerInterface $logger)
  12.     {
  13.         $this->smsProClient = $smsProClient;
  14.         $this->logger = $logger;
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             OrderPlacedEvent::class => 'onOrderPlaced',
  20.         ];
  21.     }
  22.     public function onOrderPlaced(OrderPlacedEvent $event): void
  23.     {
  24.         $this->logger->info('SMS DEBUG: OrderPlacedSubscriber declenche');
  25.         $this->logger->info('SMS DEBUG: donnees commande', [
  26.             'ticket' => $event->getReference(),
  27.             'telephone' => $event->getCustomerPhone(),
  28.             'total' => $event->getTotal(),
  29.         ]);
  30.         $telephone = $this->normalizePhone($event->getCustomerPhone());
  31.         if ($telephone === null || $telephone === '') {
  32.             $this->logger->warning('SMS skipped: missing or invalid customer phone.', [
  33.                 'ticket' => $event->getReference(),
  34.                 'phone' => $event->getCustomerPhone(),
  35.             ]);
  36.             return;
  37.         }
  38.         $message = sprintf(
  39.             'Votre commande N°%s a ete recue. Montant: %s FCFA. Merci.',
  40.             $event->getReference(),
  41.             number_format($event->getTotal(), 0, ',', ' ')
  42.         );
  43.         $lieu = trim((string) $event->getDeliveryAddress());
  44.         if ($lieu !== '') {
  45.             $message .= ' Livraison: ' . $lieu;
  46.         }
  47.         $result = $this->smsProClient->sendSms($telephone, $message);
  48.         if (!empty($result['success'])) {
  49.             $this->logger->info('Order SMS sent successfully.', [
  50.                 'ticket' => $event->getReference(),
  51.                 'recipient' => $telephone,
  52.                 'http_code' => $result['http_code'] ?? null,
  53.             ]);
  54.             return;
  55.         }
  56.         $this->logger->error('Order SMS failed.', [
  57.             'ticket' => $event->getReference(),
  58.             'recipient' => $telephone,
  59.             'http_code' => $result['http_code'] ?? null,
  60.             'error' => $result['error'] ?? null,
  61.             'raw' => $result['raw'] ?? null,
  62.         ]);
  63.     }
  64.     private function normalizePhone(?string $phone): ?string
  65.     {
  66.         $phone = trim((string) $phone);
  67.         if ($phone === '') {
  68.             return null;
  69.         }
  70.         $phone = str_replace(' ', '', $phone);
  71.         if (strpos($phone, '00') === 0) {
  72.             $phone = '+' . substr($phone, 2);
  73.         }
  74.         return $phone;
  75.     }
  76. }