-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCampaignSubscriber.php
171 lines (149 loc) · 6.38 KB
/
CampaignSubscriber.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace MauticPlugin\LeuchtfeuerPrintmailingBundle\EventListener;
use Mautic\CampaignBundle\CampaignEvents;
use Mautic\CampaignBundle\Event\CampaignBuilderEvent;
use Mautic\CampaignBundle\Event\CampaignExecutionEvent;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\IpLookupHelper;
use Mautic\CoreBundle\Model\AuditLogModel;
use MauticPlugin\LeuchtfeuerPrintmailingBundle\Entity\TriggerCampaign;
use MauticPlugin\LeuchtfeuerPrintmailingBundle\Event\TriggerCampaignEvent;
use MauticPlugin\LeuchtfeuerPrintmailingBundle\Form\Type\ActionType;
use MauticPlugin\LeuchtfeuerPrintmailingBundle\Model\TriggerCampaignModel;
use MauticPlugin\LeuchtfeuerPrintmailingBundle\PrintmailingEvents;
use MauticPlugin\LeuchtfeuerPrintmailingBundle\Service\PrintmailingService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CampaignSubscriber implements EventSubscriberInterface
{
/**
* @var IpLookupHelper
*/
protected $ipLookupHelper;
/**
* @var AuditLogModel
*/
protected $auditLogModel;
/**
* @var CoreParametersHelper
*/
protected $coreParametersHelper;
/**
* @var TriggerCampaignModel
*/
protected $triggerCampaignModel;
/**
* CampaignSubscriber constructor.
*/
public function __construct(CoreParametersHelper $coreParametersHelper, IpLookupHelper $ipLookupHelper, AuditLogModel $auditLogModel, TriggerCampaignModel $triggerCampaignModel)
{
$this->ipLookupHelper = $ipLookupHelper;
$this->auditLogModel = $auditLogModel;
$this->coreParametersHelper = $coreParametersHelper;
$this->triggerCampaignModel = $triggerCampaignModel;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
CampaignEvents::CAMPAIGN_ON_BUILD => ['onCampaignBuild', 0],
PrintmailingEvents::TRIGGER_CAMPAIGN_PRE_SAVE => ['onTriggerCampaignPreSave', 0],
PrintmailingEvents::TRIGGER_CAMPAIGN_POST_SAVE => ['onTriggerCampaignPostSave', 0],
PrintmailingEvents::TRIGGER_CAMPAIGN_PRE_DELETE => ['onTriggerCampaignPreDelete', 0],
PrintmailingEvents::ON_CAMPAIGN_TRIGGER_ACTION => ['onCampaignTriggerAction', 0],
];
}
public function onCampaignBuild(CampaignBuilderEvent $event): void
{
$event->addAction(
'plugin.printmailing.campaign',
[
'eventName' => PrintmailingEvents::ON_CAMPAIGN_TRIGGER_ACTION,
'label' => 'plugin.printmailing.campaign.label',
'description' => 'plugin.printmailing.campaign.description',
'formType' => ActionType::class,
]
);
}
/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \MauticPlugin\LeuchtfeuerPrintmailingBundle\Exception\RequestException
*/
public function onTriggerCampaignPreSave(TriggerCampaignEvent $event): void
{
$triggerCampaign = $event->getTriggerCampaign();
if ($triggerCampaign->isNew()) {
$printNodeId = time();
$triggerCampaign->setPrintNodeId('ID_'.$printNodeId);
$triggerCampaign->setPrintNodeDescription('DESC_'.$printNodeId);
} elseif ($changes = $event->getChanges()) {
if (isset($changes['name']) || isset($changes['startDate'])) {
$this->getPrintmailingService()->updateCampaign($triggerCampaign);
}
if (isset($changes['variables'])) {
$this->getPrintmailingService()->updateCampaignVariable($triggerCampaign, $changes['variables'][1]);
}
}
}
public function onTriggerCampaignPostSave(TriggerCampaignEvent $event): void
{
$triggerCampaign = $event->getTriggerCampaign();
if (isset($triggerCampaign->getChanges()['printNodeId'])) {
$triggerCampaign = $this->getPrintmailingService()->createCampaign($triggerCampaign);
$this->triggerCampaignModel->getRepository()->saveEntity($triggerCampaign);
}
if ($details = $event->getChanges()) {
$this->auditLogModel->writeToLog([
'bundle' => 'printmailing',
'object' => TriggerCampaignModel::NAME,
'objectId' => $event->getTriggercampaign()->getId(),
'details' => $details,
'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(),
]);
}
}
public function onTriggerCampaignPreDelete(TriggerCampaignEvent $event): void
{
// here you can the API call if there is a possibility to remove/hide mailing within Deutschepost API.
}
public function onCampaignTriggerAction(CampaignExecutionEvent $event)
{
if (false === $event->checkContext('plugin.printmailing.campaign')) {
return;
}
$config = $event->getConfig();
if (!isset($config['trigger_campaign']) || empty($config['trigger_campaign'])) {
$event->setFailed('No trigger campaign found for given ID.');
return;
}
$triggerCampaign = $this->triggerCampaignModel->getEntity($config['trigger_campaign']);
if (!$triggerCampaign instanceof TriggerCampaign) {
$event->setFailed('Could not find matching campaign ID.');
return;
}
try {
$lead = $event->getLead();
$this->auditLogModel->writeToLog([
'bundle' => 'printmailing',
'object' => 'lead',
'objectId' => $lead->getId(),
'action' => 'registered for campaign',
'details' => $event->getEventSettings(),
'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(),
]);
$this->getPrintmailingService()->createRecipient($triggerCampaign, $lead);
$event->setResult(true);
} catch (\Exception $exception) {
$event->setFailed($exception->getMessage());
}
}
protected function getPrintmailingService(): PrintmailingService
{
return PrintmailingService::makeInstance(
(int) $this->coreParametersHelper->get('printmailing_masId'),
$this->coreParametersHelper->get('printmailing_masClientId'),
$this->coreParametersHelper->get('printmailing_rest_password')
);
}
}