示例#1
0
 /**
  * @param CampaignExecutionEvent $event
  */
 public function onCampaignTriggerAction(CampaignExecutionEvent $event)
 {
     $lead = $event->getLead();
     if ($this->leadModel->isContactable($lead, 'notification') !== DoNotContact::IS_CONTACTABLE) {
         return $event->setFailed('mautic.notification.campaign.failed.not_contactable');
     }
     // If lead has subscribed on multiple devices, get all of them.
     /** @var \Mautic\NotificationBundle\Entity\PushID[] $pushIDs */
     $pushIDs = $lead->getPushIDs();
     $playerID = [];
     foreach ($pushIDs as $pushID) {
         $playerID[] = $pushID->getPushID();
     }
     if (empty($playerID)) {
         return $event->setFailed('mautic.notification.campaign.failed.not_subscribed');
     }
     $notificationId = (int) $event->getConfig()['notification'];
     /** @var \Mautic\NotificationBundle\Entity\Notification $notification */
     $notification = $this->notificationModel->getEntity($notificationId);
     if ($notification->getId() !== $notificationId) {
         return $event->setFailed('mautic.notification.campaign.failed.missing_entity');
     }
     if ($url = $notification->getUrl()) {
         $url = $this->notificationApi->convertToTrackedUrl($url, ['notification' => $notification->getId(), 'lead' => $lead->getId()]);
     }
     $tokenEvent = $this->dispatcher->dispatch(NotificationEvents::TOKEN_REPLACEMENT, new TokenReplacementEvent($notification->getMessage(), $lead, ['channel' => ['notification', $notification->getId()]]));
     $sendEvent = $this->dispatcher->dispatch(NotificationEvents::NOTIFICATION_ON_SEND, new NotificationSendEvent($tokenEvent->getContent(), $notification->getHeading(), $lead));
     $response = $this->notificationApi->sendNotification($playerID, $sendEvent->getMessage(), $sendEvent->getHeading(), $url);
     $event->setChannel('notification', $notification->getId());
     // If for some reason the call failed, tell mautic to try again by return false
     if ($response->code !== 200) {
         return $event->setResult(false);
     }
     $this->notificationModel->createStatEntry($notification, $lead);
     $this->notificationModel->getRepository()->upCount($notificationId);
     $result = ['status' => 'mautic.notification.timeline.status.delivered', 'type' => 'mautic.notification.notification', 'id' => $notification->getId(), 'name' => $notification->getName(), 'heading' => $event->getHeading(), 'content' => $event->getMessage()];
     $event->setResult($result);
 }