<?php
/**
* @author Antony Tkachenko <at@canaryknight.ru>
*/
namespace App\Event\Form;
use App\Form\Contracts\CreateGmbProjectRequest;
use App\Form\FormHandledEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreateGmbProjectSubscriber implements EventSubscriberInterface
{
/** @var string|null */
private $crmUri;
public function __construct(?string $crmUri)
{
$this->crmUri = $crmUri;
}
public function createGmbProject(FormHandledEvent $event)
{
if (!$this->crmUri) {
return;
}
$dto = $event->getDto();
if (!is_a($dto, CreateGmbProjectRequest::class)) {
return;
}
try {
$sendData = $dto->getGmbData();
$dataString = json_encode($sendData);
$ch = curl_init("{$this->crmUri}/request/create-request");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($dataString))
);
$result = curl_exec($ch);
curl_close($ch);
} catch (\Throwable $e) {
}
}
public static function getSubscribedEvents()
{
return [
FormHandledEvent::NAME => 'createGmbProject'
];
}
}