<?php
namespace App\Controller\Webhook;
use App\Traits\SentryNotifyTrait;
use Doctrine\ORM\EntityManagerInterface;
use Evo\Infrastructure\MappingORM\CustomerIoEvent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CustomerIoController extends AbstractController
{
use SentryNotifyTrait;
private Response $response;
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->response = new Response();
$this->em = $em;
}
/**
* @Route("/customerio", name="app_webhook_customerio", methods={"POST"})
*/
public function webhook()
{
$requestBody = file_get_contents('php://input');
$customerIoData = json_decode($requestBody, null, 512, JSON_THROW_ON_ERROR);
if ($customerIoData && $customerIoData->data) {
$customerRecipient = isset($customerIoData->data->recipient) && $customerIoData->data->recipient ?: '';
$customerId = $customerIoData->data->customer_id ?: '';
$customerIoEvent = new CustomerIoEvent();
$customerIoEvent->setCustomerId($customerId);
$customerIoEvent->setMetric($customerIoData->metric);
$customerIoEvent->setObjectType($customerIoData->object_type);
$customerIoEvent->setTimestamp($customerIoData->timestamp);
$customerIoEvent->setRecipient($customerRecipient);
$this->em->persist($customerIoEvent);
$this->em->flush();
}
$this->response->setStatusCode(200);
return $this->response->send();
}
}