<?php
namespace App\EventSubscriber;
use App\DTO\Popup\Configuration;
use App\Entity\CampaignItem;
use App\Entity\ContactCampaignJob;
use App\Entity\PopupStatistic;
use App\Entity\PopupStatisticType;
use App\Entity\PopupType;
use App\Event\ContactCreatedEvent;
use App\Repository\PopupStatisticTypeRepository;
use App\Repository\UserRepository;
use App\Service\Manager\ContactCampaignJobManager;
use App\Service\Manager\PopupManager;
use App\Service\Manager\PopupStatisticManager;
use App\Service\SiteNotificationSender;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContactSubscriber implements EventSubscriberInterface
{
/**
* @var SiteNotificationSender
*/
private $sender;
/**
* @var PopupStatisticTypeRepository
*/
private $popupStatisticTypeRepository;
/**
* @var PopupStatisticManager
*/
private $popupStatisticManager;
/**
* @var UserRepository
*/
private $userRepository;
/**
* @var PopupManager
*/
private $popupManager;
/**
* @var ContactCampaignJobManager
*/
private $contactCampaignJobManager;
public function __construct(
SiteNotificationSender $sender,
PopupStatisticTypeRepository $popupStatisticTypeRepository,
PopupStatisticManager $popupStatisticManager,
UserRepository $userRepository,
PopupManager $popupManager,
ContactCampaignJobManager $contactCampaignJobManager
)
{
$this->sender = $sender;
$this->popupStatisticTypeRepository = $popupStatisticTypeRepository;
$this->popupStatisticManager = $popupStatisticManager;
$this->userRepository = $userRepository;
$this->popupManager = $popupManager;
$this->contactCampaignJobManager = $contactCampaignJobManager;
}
public static function getSubscribedEvents()
{
return [
ContactCreatedEvent::class => [
['sendNotifications'],
['addStatistic'],
['addCampaignJobs'],
],
];
}
public function sendNotifications(ContactCreatedEvent $event)
{
$contact = $event->getContact();
$popup = $contact->getPopup();
$configuration = $popup->getConfigurationDto();
if ($configuration->senderTarget === Configuration::TARGET_MEMBER) {
foreach ($this->userRepository->findBy(['id' => $configuration->memberReceivers]) as $user) {
$this->sender->sendContactNotificationToUser($user, $contact);
}
} elseif($configuration->senderTarget === Configuration::TARGET_CUSTOM_EMAILS) {
if (null !== $configuration->emailReceivers) {
$this->sender->sendContactNotification($configuration->emailReceivers, $contact);
}
}
$items = $contact->getItems();
if ($configuration->sendToClient
&& isset($items['email'])
&& !empty($items['email'])) {
$this->sender->sendContactCallback($items['email'], $contact);
}
}
public function addStatistic(ContactCreatedEvent $event)
{
$contact = $event->getContact();
$popupStatistic = new PopupStatistic();
$popupStatistic->setPopup($contact->getPopup());
$popupStatistic->setType($this->popupStatisticTypeRepository->find(PopupStatisticType::TYPE_MESSAGE));
$popupStatistic->setGuid($contact->getGuid());
$popupStatistic->setReceivedAt(new \DateTime());
if (null !== $contact->getIp()) {
$popupStatistic->setIp($contact->getIp());
}
$this->popupStatisticManager->create($popupStatistic);
}
public function addCampaignJobs(ContactCreatedEvent $event)
{
$contact = $event->getContact();
$popup = $contact->getPopup();
if ($this->popupManager->isCampaignAvailable($popup)) {
$plannedAt = clone $contact->getCreatedAt();
foreach ($popup->getCampaignItems() as $campaignItem) {
if (CampaignItem::TYPE_START === $campaignItem->getType()) {
$data = $campaignItem->getData();
$plannedAt->modify('+1 day')->setTime($data['hour'], $data['minute']);
} elseif (CampaignItem::TYPE_WAIT === $campaignItem->getType()) {
$data = $campaignItem->getData();
$plannedAt->modify(sprintf('+%s day', $data['days']));
} else {
$this->contactCampaignJobManager->create(
$contact,
$campaignItem->getType(),
$plannedAt,
$campaignItem->getData()
);
}
}
}
}
}