Spaces:
No application file
No application file
File size: 7,567 Bytes
d2897cd |
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
<?php
namespace Mautic\CampaignBundle\Executioner;
use Doctrine\Common\Collections\ArrayCollection;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Entity\EventRepository;
use Mautic\CampaignBundle\EventCollector\Accessor\Event\DecisionAccessor;
use Mautic\CampaignBundle\EventCollector\EventCollector;
use Mautic\CampaignBundle\Executioner\Event\DecisionExecutioner as Executioner;
use Mautic\CampaignBundle\Executioner\Exception\CampaignNotExecutableException;
use Mautic\CampaignBundle\Executioner\Exception\DecisionNotApplicableException;
use Mautic\CampaignBundle\Executioner\Helper\DecisionHelper;
use Mautic\CampaignBundle\Executioner\Result\Responses;
use Mautic\CampaignBundle\Executioner\Scheduler\EventScheduler;
use Mautic\CampaignBundle\Helper\ChannelExtractor;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
use Mautic\LeadBundle\Tracker\ContactTracker;
use Psr\Log\LoggerInterface;
class RealTimeExecutioner
{
/**
* @var Lead
*/
private $contact;
/**
* @var array
*/
private $events;
private ?Responses $responses = null;
public function __construct(
private LoggerInterface $logger,
private LeadModel $leadModel,
private EventRepository $eventRepository,
private EventExecutioner $executioner,
private Executioner $decisionExecutioner,
private EventCollector $collector,
private EventScheduler $scheduler,
private ContactTracker $contactTracker,
private DecisionHelper $decisionHelper
) {
}
/**
* @param string $type
* @param mixed $passthrough
* @param string|null $channel
* @param int|null $channelId
*
* @return Responses
*
* @throws Dispatcher\Exception\LogNotProcessedException
* @throws Dispatcher\Exception\LogPassedAndFailedException
* @throws Exception\CannotProcessEventException
* @throws Scheduler\Exception\NotSchedulableException
*/
public function execute($type, $passthrough = null, $channel = null, $channelId = null)
{
$this->responses = new Responses();
$now = new \DateTime();
$this->logger->debug('CAMPAIGN: Campaign triggered for event type '.$type.'('.$channel.' / '.$channelId.')');
// Kept for BC support although not sure we need this
defined('MAUTIC_CAMPAIGN_NOT_SYSTEM_TRIGGERED') or define('MAUTIC_CAMPAIGN_NOT_SYSTEM_TRIGGERED', 1);
try {
$this->fetchCurrentContact();
} catch (CampaignNotExecutableException $exception) {
$this->logger->debug('CAMPAIGN: '.$exception->getMessage());
return $this->responses;
}
try {
$this->fetchCampaignData($type);
} catch (CampaignNotExecutableException $exception) {
$this->logger->debug('CAMPAIGN: '.$exception->getMessage());
return $this->responses;
}
/** @var Event $event */
foreach ($this->events as $event) {
try {
$this->evaluateDecisionForContact($event, $passthrough, $channel, $channelId);
} catch (DecisionNotApplicableException $exception) {
$this->logger->debug('CAMPAIGN: Event ID '.$event->getId().' is not applicable ('.$exception->getMessage().')');
continue;
}
$children = $event->getPositiveChildren();
if (!$children->count()) {
$this->logger->debug('CAMPAIGN: Event ID '.$event->getId().' has no positive children');
continue;
}
$this->executeAssociatedEvents($children, $now);
}
// Save any changes to the contact done by the listeners
if ($this->contact->getChanges()) {
$this->leadModel->saveEntity($this->contact, false);
}
return $this->responses;
}
/**
* @throws Dispatcher\Exception\LogNotProcessedException
* @throws Dispatcher\Exception\LogPassedAndFailedException
* @throws Exception\CannotProcessEventException
* @throws Scheduler\Exception\NotSchedulableException
*/
private function executeAssociatedEvents(ArrayCollection $children, \DateTime $now): void
{
$children = clone $children;
/** @var Event $child */
foreach ($children as $key => $child) {
$executionDate = $this->scheduler->getExecutionDateTime($child, $now);
$this->logger->debug(
'CAMPAIGN: Event ID# '.$child->getId().
' to be executed on '.$executionDate->format('Y-m-d H:i:s e')
);
if ($this->scheduler->shouldSchedule($executionDate, $now)) {
$this->scheduler->scheduleForContact($child, $executionDate, $this->contact);
$children->remove($key);
}
}
if ($children->count()) {
$this->executioner->executeEventsForContact($children, $this->contact, $this->responses);
}
}
/**
* @param mixed $passthrough
* @param string|null $channel
* @param int|null $channelId
*
* @throws DecisionNotApplicableException
* @throws Exception\CannotProcessEventException
*/
private function evaluateDecisionForContact(Event $event, $passthrough = null, $channel = null, $channelId = null): void
{
$this->logger->debug('CAMPAIGN: Executing '.$event->getType().' ID '.$event->getId().' for contact ID '.$this->contact->getId());
$this->decisionHelper->checkIsDecisionApplicableForContact($event, $this->contact, $channel, $channelId);
/** @var DecisionAccessor $config */
$config = $this->collector->getEventConfig($event);
$this->decisionExecutioner->evaluateForContact($config, $event, $this->contact, $passthrough, $channel, $channelId);
}
/**
* @throws CampaignNotExecutableException
*/
private function fetchCurrentContact(): void
{
$this->contact = $this->contactTracker->getContact();
if (!$this->contact instanceof Lead || !$this->contact->getId()) {
throw new CampaignNotExecutableException('Unidentifiable contact');
}
$this->logger->debug('CAMPAIGN: Current contact ID# '.$this->contact->getId());
}
/**
* @throws CampaignNotExecutableException
*/
private function fetchCampaignData($type): void
{
if (!$this->events = $this->eventRepository->getContactPendingEvents($this->contact->getId(), $type)) {
throw new CampaignNotExecutableException('Contact does not have any applicable '.$type.' associations.');
}
// 2.14 BC break workaround - pre 2.14 had a bug that recorded channelId for decisions as 1 regardless of actually ID
// if channelIdField was an array and only one item was selected. That caused the channel ID check in evaluateDecisionForContact
// to fail resulting in the decision never being evaluated. Therefore we are going to self heal these decisions.
/** @var Event $event */
foreach ($this->events as $event) {
if (1 === $event->getChannelId()) {
ChannelExtractor::setChannel($event, $event, $this->collector->getEventConfig($event));
$this->eventRepository->saveEntity($event);
}
}
$this->logger->debug('CAMPAIGN: Found '.count($this->events).' events to analyze for contact ID '.$this->contact->getId());
}
}
|