Spaces:
No application file
No application file
File size: 3,541 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 |
<?php
declare(strict_types=1);
namespace Mautic\MessengerBundle\MessageHandler;
use Mautic\LeadBundle\Entity\LeadRepository;
use Mautic\MessengerBundle\Exceptions\InvalidPayloadException;
use Mautic\MessengerBundle\Message\PageHitNotification;
use Mautic\PageBundle\Entity\Hit;
use Mautic\PageBundle\Entity\HitRepository;
use Mautic\PageBundle\Entity\PageRepository;
use Mautic\PageBundle\Entity\RedirectRepository;
use Mautic\PageBundle\Model\PageModel;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\Acknowledger;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
class PageHitNotificationHandler implements MessageHandlerInterface
{
public function __construct(
private PageRepository $pageRepository,
private HitRepository $hitRepository,
private LeadRepository $leadRepository,
private LoggerInterface $logger,
private RedirectRepository $redirectRepository,
private PageModel $pageModel
) {
}
/**
* @throws InvalidPayloadException
*/
public function __invoke(PageHitNotification $message, Acknowledger $ack = null): void
{
$parsed = $this->parseMessage($message);
$this->pageModel->processPageHit(...$parsed);
$this->logger->info('processed page hit #'.$message->getHitId());
}
/**
* @return array<string, mixed>
*
* @throws InvalidPayloadException
*/
private function parseMessage(PageHitNotification $message): array
{
$hit = $message->getHitId() > 0 ? $this->hitRepository->find($message->getHitId()) : null;
$pageObject = null;
if (null !== $message->getPageId()) {
try {
$pageObject = $message->isRedirect()
? $this->redirectRepository->find($message->getPageId())
: $this->pageRepository->find($message->getPageId());
} catch (\Exception $exception) {
$this->logger->error(
sprintf('Invalid page/redirect, exception. #%s', $message->getPageId()),
['message' => $message]
);
throw $exception;
}
if (null === $pageObject) {
$this->logger->error(
sprintf('Invalid page/redirect, id not found. #%s', $message->getPageId())
);
throw new InvalidPayloadException('Missing required information', ['message' => $message]);
}
}
if (!$hit instanceof Hit && $message->getHitId() > 0) {
$this->logger->warning('Invalid hit id #'.$message->getHitId(), ['message' => $message]);
throw new InvalidPayloadException('Invalid hit id #'.$message->getHitId(), (array) $message);
}
// Lead IS mandatory field
if (null === $lead = $this->leadRepository->find($message->getLeadId())) {
$this->logger->error('Invalid lead id #'.$message->getLeadId(), ['message' => $message]);
throw new InvalidPayloadException('Invalid lead id', (array) $message);
}
return [
'hit' => $hit,
'page' => $pageObject,
'request' => $message->getRequest(),
'lead' => $lead,
'trackingNewlyGenerated' => $message->isNew(),
'activeRequest' => false,
'hitDate' => $message->getEventTime(),
];
}
}
|