src/EventListener/ApplyWithWhatsappEventListener.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Event\ApplyWithWhatsappEvent;
  4. use Exception;
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\RequestOptions;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. class ApplyWithWhatsappEventListener
  9. {
  10.     /**
  11.      * @var Client
  12.      */
  13.     private $client;
  14.     /**
  15.      * @var ParameterBagInterface
  16.      */
  17.     private $parameterBag;
  18.     public function __construct(ParameterBagInterface $parameterBag)
  19.     {
  20.         $this->parameterBag $parameterBag;
  21.         $this->client = new Client([
  22.             'base_uri' => $this->parameterBag->get('site_apply_with_whatsapp_url'),
  23.         ]);
  24.     }
  25.     public function onAppliedWithWhatsapp(ApplyWithWhatsappEvent $event)
  26.     {
  27.         if (!$this->checkRequiredParameters()) {
  28.             return;
  29.         }
  30.         try {
  31.             $this->client->post('whatsapp', [
  32.                 RequestOptions::JSON => [
  33.                     'chatbot' => $this->parameterBag->get('site_apply_with_whatsapp_chatbot'),
  34.                     'token' => 'Token '.$this->parameterBag->get('site_apply_with_whatsapp_token'),
  35.                     'flow' => $this->parameterBag->get('site_apply_with_whatsapp_flow_id'),
  36.                     'phone_number' => $event->getApplyWithWhatsapp()->getPhone(),
  37.                     'template_name' => $this->parameterBag->get('site_apply_with_whatsapp_template_name'),
  38.                     'template_params' => ['1' => []],
  39.                 ],
  40.             ]);
  41.         } catch (Exception $exception) {
  42.         }
  43.     }
  44.     private function checkRequiredParameters(): bool
  45.     {
  46.         if (empty($this->parameterBag->get('site_apply_with_whatsapp_url'))) {
  47.             return false;
  48.         }
  49.         if (empty($this->parameterBag->get('site_apply_with_whatsapp_chatbot'))) {
  50.             return false;
  51.         }
  52.         if (empty($this->parameterBag->get('site_apply_with_whatsapp_token'))) {
  53.             return false;
  54.         }
  55.         if (empty($this->parameterBag->get('site_apply_with_whatsapp_flow_id'))) {
  56.             return false;
  57.         }
  58.         if (empty($this->parameterBag->get('site_apply_with_whatsapp_template_name'))) {
  59.             return false;
  60.         }
  61.         return true;
  62.     }
  63. }