<?php
namespace App\Controller\Api;
use App\Service\SegmentAPI;
use App\Traits\SentryNotifyTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/segment")
*/
class SegmentController extends AbstractController
{
use SentryNotifyTrait;
private SegmentAPI $segmentAPI;
public function __construct(SegmentAPI $segmentAPI)
{
$this->segmentAPI = $segmentAPI;
}
/**
* @Route("/track", name="app_segment_track", methods={"POST","GET"})
*
* @return JsonResponse
*/
public function track(Request $request)
{
$query = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$admin = $request->get('email');
try {
$this->segmentAPI->track($query, $admin);
return $this->json(['message' => 'Track sent']);
} catch (\Exception $e) {
$this->captureSentryException($e);
return $this->json(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* @Route("/identify", name="app_segment_identify", methods={"POST","GET"})
*
* @return JsonResponse
*/
public function identify(Request $request)
{
$query = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$admin = $request->get('email');
try {
$this->segmentAPI->identify($query, $admin);
return $this->json(['message' => 'Track sent']);
} catch (\Exception $e) {
$this->captureSentryException($e);
return $this->json(['message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}