src/Event/Form/AmoCrmSubscriber.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\AmoCrm\Leads;
  7. use App\Form\FormHandledEvent;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class AmoCrmSubscriber implements EventSubscriberInterface
  11. {
  12.     private $logger;
  13.     public function __construct(LoggerInterface $logger)
  14.     {
  15.         $this->logger $logger;
  16.     }
  17.     public function createLead(FormHandledEvent $event)
  18.     {
  19.         $dto $event->getDto();
  20.         $data $event->getData();
  21.         $comment '';
  22.         foreach ($data as $label => $val) {
  23.             if( $label == 'Пароль'){
  24.                 continue;
  25.             }
  26.             $comment .= "{$label}{$val}\n";
  27.         }
  28.         $comment trim($comment);
  29.         $formName 'gmb_' $event->getFormName();
  30.         $utm $event->getUtm();
  31.         try {
  32.             Leads::addUnsorted(
  33.                 $dto->subject(),
  34.                 $formName,
  35.                 $utm,
  36.                 $dto->{'name'} ?? null,
  37.                 $dto->{'phone'} ?? null,
  38.                 $dto->{'email'} ?? null,
  39.                 $comment
  40.             );
  41.         } catch (\Throwable $e) {
  42.             $this->logger->error($e->getMessage());
  43.         }
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             FormHandledEvent::NAME => 'createLead'
  49.         ];
  50.     }
  51. }