<?php
/**
* @author Antony Tkachenko <at@canaryknight.ru>
*/
namespace App\Event\Form;
use App\AmoCrm\Leads;
use App\Form\FormHandledEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AmoCrmSubscriber implements EventSubscriberInterface
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function createLead(FormHandledEvent $event)
{
$dto = $event->getDto();
$data = $event->getData();
$comment = '';
foreach ($data as $label => $val) {
if( $label == 'Пароль'){
continue;
}
$comment .= "{$label}: {$val}\n";
}
$comment = trim($comment);
$formName = 'gmb_' . $event->getFormName();
$utm = $event->getUtm();
try {
Leads::addUnsorted(
$dto->subject(),
$formName,
$utm,
$dto->{'name'} ?? null,
$dto->{'phone'} ?? null,
$dto->{'email'} ?? null,
$comment
);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage());
}
}
public static function getSubscribedEvents()
{
return [
FormHandledEvent::NAME => 'createLead'
];
}
}