<?php
/**
* @author Antony Tkachenko <at@canaryknight.ru>
*/
namespace App\Event\Form;
use App\Form\Contracts\UniSender;
use App\Form\FormHandledEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UniSenderSubscriber implements EventSubscriberInterface
{
/** @var string|null */
private $token;
public function __construct(?string $token)
{
$this->token = $token;
}
public function subscribe(FormHandledEvent $event)
{
if (!$this->token) {
return;
}
$dto = $event->getDto();
if (!is_a($dto, UniSender::class)) {
return;
}
try {
$data = [
'format' => 'json',
'api_key' => $this->token,
'list_ids' => 4,
'fields' => [
'email' => $dto->getEmail()
],
'double_optin' => 3,
];
$testUrl = 'https://api.unisender.com/ru/api/subscribe?' . http_build_query($data);
$res = file_get_contents($testUrl);
} catch (\Exception $e) {}
}
public static function getSubscribedEvents()
{
return [
FormHandledEvent::NAME => 'subscribe'
];
}
}