-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSameSubjectFollowupSubscriber.php
129 lines (116 loc) · 4.48 KB
/
SameSubjectFollowupSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php declare(strict_types=1);
namespace MauticPlugin\MauticColdEmailOutreachBundle\EventListener;
use Mautic\CampaignBundle\CampaignEvents;
use Mautic\CampaignBundle\Entity\Campaign;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Entity\LeadEventLog;
use Mautic\CampaignBundle\Entity\LeadEventLogRepository;
use Mautic\CampaignBundle\Event\CampaignTriggerEvent;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Entity\EmailRepository;
use Mautic\EmailBundle\Entity\Stat;
use Mautic\EmailBundle\Entity\StatRepository;
use Mautic\EmailBundle\Event\EmailSendEvent;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use Mautic\PluginBundle\Integration\AbstractIntegration;
use MauticPlugin\MauticColdEmailOutreachBundle\Integration\ColdEmailOutreachIntegration;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SameSubjectFollowupSubscriber implements EventSubscriberInterface
{
private $enabled;
private $eventLogRepository;
private $emailRepository;
private $statRepository;
/** @var Campaign|null */
private $campaign;
public function __construct(IntegrationHelper $integrationHelper,
LeadEventLogRepository $eventLogRepository,
EmailRepository $emailRepository,
StatRepository $statRepository
)
{
$integrationObject = $integrationHelper->getIntegrationObject(ColdEmailOutreachIntegration::INTEGRATION_NAME);
$this->enabled = $integrationObject instanceof AbstractIntegration && $integrationObject->getIntegrationSettings()->getIsPublished();
$this->eventLogRepository = $eventLogRepository;
$this->emailRepository = $emailRepository;
$this->statRepository = $statRepository;
}
public static function getSubscribedEvents(): array
{
return [
CampaignEvents::CAMPAIGN_ON_TRIGGER => ['onCampaignTriggered', 0],
EmailEvents::EMAIL_ON_SEND => ['onEmailSend', 255],
];
}
public function onCampaignTriggered(CampaignTriggerEvent $event)
{
if (!$this->enabled) {
return;
}
$this->campaign = $event->getCampaign();
}
public function onEmailSend(EmailSendEvent $event)
{
if (!$this->enabled || null === $this->campaign || null === $leadId = $this->getLeadId($event)) {
return;
}
$email = $this->getFirstSentEmailVariant($this->campaign, $leadId);
if (null !== $email) {
$event->setSubject('Re: '.$email->getSubject());
}
}
private function getFirstSentEmailVariant(Campaign $campaign, int $leadId): ?Email
{
/** @var LeadEventLog|null $firstSentEmailWithinCampaign */
$firstSentEmailWithinCampaign = $this->eventLogRepository->findOneBy([
'lead' => $leadId,
'campaign' => $campaign,
'channel' => 'email',
], ['dateTriggered' => 'ASC']);
if (null === $firstSentEmailWithinCampaign) {
return null;
}
/** @var Email $firstEmail */
$firstEmail = $this->emailRepository->find($firstSentEmailWithinCampaign->getChannelId());
if (null === $firstEmail) {
return null;
}
/** @var Stat $stat */
$stat = $this->statRepository->findOneBy([
'email' => $this->getEmailVariants($firstEmail),
'lead' => $leadId,
'isFailed' => false,
'source' => 'campaign.event',
'sourceId' => $this->getCampaignEvents($campaign),
], ['dateSent' => 'ASC']);
return null !== $stat ? $stat->getEmail() : null;
}
private function getEmailVariants(Email $email): array
{
$ids = [$email->getId()];
/** @var Email $variantChild */
foreach ($email->getVariantChildren() as $variantChild) {
$ids[] = $variantChild->getId();
}
return $ids;
}
private function getCampaignEvents(Campaign $campaign): array
{
$ids = [];
/** @var Event $event */
foreach ($campaign->getEvents() as $event) {
$ids[] = $event->getId();
}
return $ids;
}
private function getLeadId(EmailSendEvent $event): ?int
{
$lead = $event->getLead();
if ($lead instanceof Lead) {
return $lead->getId();
}
return isset ($lead['id']) ? (int) $lead['id'] : null;
}
}