<?php
namespace App\Controller\Marketing;
use App\Service\Marketing\MarketingImageService;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
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("/api/public/marketing/image")
*/
class PublicMarketingImageController extends AbstractController
{
/**
* PublicMarketingImageController constructor.
*/
public function __construct(private MarketingImageService $service, private SerializerInterface $serializer)
{
}
/**
* @SWG\Get(tags={"Bonus"},
*
* @SWG\Response(response=200, description="Get current marketing image", @SWG\Schema(ref=@Model(type=App\Entity\BonusImage::class, groups={"bonusindex"}))),
* @SWG\Response(response=404, description="If there is no current marketing image")
* )
*
* @Route("/current", methods={"GET"})
*
* @throws \Exception
*/
public function index(Request $request): JsonResponse
{
$marketingImage = $this->service->getCurrentMarketingImage(new \DateTime());
if (!$marketingImage) {
return new JsonResponse([
'success' => false,
], Response::HTTP_NOT_FOUND);
}
$context = SerializationContext::create()->setGroups([
'bonusindex',
'frontendindex',
]);
$result = [
'marketingImage' => $marketingImage,
];
return JsonResponse::fromJsonString($this->serializer->serialize($result, 'json', $context));
}
}