src/EventSubscriber/ContactSubscriber.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\DTO\Popup\Configuration;
  4. use App\Entity\CampaignItem;
  5. use App\Entity\ContactCampaignJob;
  6. use App\Entity\PopupStatistic;
  7. use App\Entity\PopupStatisticType;
  8. use App\Entity\PopupType;
  9. use App\Event\ContactCreatedEvent;
  10. use App\Repository\PopupStatisticTypeRepository;
  11. use App\Repository\UserRepository;
  12. use App\Service\Manager\ContactCampaignJobManager;
  13. use App\Service\Manager\PopupManager;
  14. use App\Service\Manager\PopupStatisticManager;
  15. use App\Service\SiteNotificationSender;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ContactSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var SiteNotificationSender
  21.      */
  22.     private $sender;
  23.     /**
  24.      * @var PopupStatisticTypeRepository
  25.      */
  26.     private $popupStatisticTypeRepository;
  27.     /**
  28.      * @var PopupStatisticManager
  29.      */
  30.     private $popupStatisticManager;
  31.     /**
  32.      * @var UserRepository
  33.      */
  34.     private $userRepository;
  35.     /**
  36.      * @var PopupManager
  37.      */
  38.     private $popupManager;
  39.     /**
  40.      * @var ContactCampaignJobManager
  41.      */
  42.     private $contactCampaignJobManager;
  43.     public function __construct(
  44.         SiteNotificationSender $sender,
  45.         PopupStatisticTypeRepository $popupStatisticTypeRepository,
  46.         PopupStatisticManager $popupStatisticManager,
  47.         UserRepository $userRepository,
  48.         PopupManager $popupManager,
  49.         ContactCampaignJobManager $contactCampaignJobManager
  50.     )
  51.     {
  52.         $this->sender $sender;
  53.         $this->popupStatisticTypeRepository $popupStatisticTypeRepository;
  54.         $this->popupStatisticManager $popupStatisticManager;
  55.         $this->userRepository $userRepository;
  56.         $this->popupManager $popupManager;
  57.         $this->contactCampaignJobManager $contactCampaignJobManager;
  58.     }
  59.     public static function getSubscribedEvents()
  60.     {
  61.         return [
  62.             ContactCreatedEvent::class => [
  63.                 ['sendNotifications'],
  64.                 ['addStatistic'],
  65.                 ['addCampaignJobs'],
  66.             ],
  67.         ];
  68.     }
  69.     public function sendNotifications(ContactCreatedEvent $event)
  70.     {
  71.         $contact $event->getContact();
  72.         $popup $contact->getPopup();
  73.         $configuration $popup->getConfigurationDto();
  74.         if ($configuration->senderTarget === Configuration::TARGET_MEMBER) {
  75.             foreach ($this->userRepository->findBy(['id' => $configuration->memberReceivers]) as $user) {
  76.                 $this->sender->sendContactNotificationToUser($user$contact);
  77.             }
  78.         } elseif($configuration->senderTarget === Configuration::TARGET_CUSTOM_EMAILS) {
  79.             if (null !== $configuration->emailReceivers) {
  80.                 $this->sender->sendContactNotification($configuration->emailReceivers$contact);
  81.             }
  82.         }
  83.         $items $contact->getItems();
  84.         if ($configuration->sendToClient
  85.             && isset($items['email'])
  86.             && !empty($items['email'])) {
  87.             $this->sender->sendContactCallback($items['email'], $contact);
  88.         }
  89.     }
  90.     public function addStatistic(ContactCreatedEvent $event)
  91.     {
  92.         $contact $event->getContact();
  93.         $popupStatistic = new PopupStatistic();
  94.         $popupStatistic->setPopup($contact->getPopup());
  95.         $popupStatistic->setType($this->popupStatisticTypeRepository->find(PopupStatisticType::TYPE_MESSAGE));
  96.         $popupStatistic->setGuid($contact->getGuid());
  97.         $popupStatistic->setReceivedAt(new \DateTime());
  98.         if (null !== $contact->getIp()) {
  99.             $popupStatistic->setIp($contact->getIp());
  100.         }
  101.         $this->popupStatisticManager->create($popupStatistic);
  102.     }
  103.     public function addCampaignJobs(ContactCreatedEvent $event)
  104.     {
  105.         $contact $event->getContact();
  106.         $popup $contact->getPopup();
  107.         if ($this->popupManager->isCampaignAvailable($popup)) {
  108.             $plannedAt = clone $contact->getCreatedAt();
  109.             foreach ($popup->getCampaignItems() as $campaignItem) {
  110.                 if (CampaignItem::TYPE_START === $campaignItem->getType()) {
  111.                     $data $campaignItem->getData();
  112.                     $plannedAt->modify('+1 day')->setTime($data['hour'], $data['minute']);
  113.                 } elseif (CampaignItem::TYPE_WAIT === $campaignItem->getType()) {
  114.                     $data $campaignItem->getData();
  115.                     $plannedAt->modify(sprintf('+%s day'$data['days']));
  116.                 } else {
  117.                     $this->contactCampaignJobManager->create(
  118.                         $contact,
  119.                         $campaignItem->getType(),
  120.                         $plannedAt,
  121.                         $campaignItem->getData()
  122.                     );
  123.                 }
  124.             }
  125.         }
  126.     }
  127. }