src/Event/Form/CreateGmbProjectSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Antony Tkachenko <at@canaryknight.ru>
  4.  */
  5. namespace App\Event\Form;
  6. use App\Form\Contracts\CreateGmbProjectRequest;
  7. use App\Form\FormHandledEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CreateGmbProjectSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var string|null */
  12.     private $crmUri;
  13.     public function __construct(?string $crmUri)
  14.     {
  15.         $this->crmUri $crmUri;
  16.     }
  17.     public function createGmbProject(FormHandledEvent $event)
  18.     {
  19.         if (!$this->crmUri) {
  20.             return;
  21.         }
  22.         $dto $event->getDto();
  23.         if (!is_a($dtoCreateGmbProjectRequest::class)) {
  24.             return;
  25.         }
  26.         try {
  27.             $sendData $dto->getGmbData();
  28.             $dataString json_encode($sendData);
  29.             $ch curl_init("{$this->crmUri}/request/create-request");
  30.             curl_setopt($chCURLOPT_CUSTOMREQUEST"POST");
  31.             curl_setopt($chCURLOPT_POSTFIELDS$dataString);
  32.             curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  33.             curl_setopt($chCURLOPT_HTTPHEADER, array(
  34.                     'Content-Type: application/json',
  35.                     'Content-Length: ' strlen($dataString))
  36.             );
  37.             $result curl_exec($ch);
  38.             curl_close($ch);
  39.         } catch (\Throwable $e) {
  40.         }
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             FormHandledEvent::NAME => 'createGmbProject'
  46.         ];
  47.     }
  48. }