<?php
namespace App\Controller\Marketing;
use App\Service\Bonus\BonusService;
use App\Service\Bonus\ImageService;
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/bonus")
*/
class PublicBonusController extends AbstractController
{
protected BonusService $service;
protected ImageService $imageService;
protected SerializerInterface $serializer;
/**
* PublicBonusController constructor.
*/
public function __construct(BonusService $service, ImageService $imageService, SerializerInterface $serializer)
{
$this->service = $service;
$this->imageService = $imageService;
$this->serializer = $serializer;
}
/**
* @SWG\Get(tags={"Bonus"},
*
* @SWG\Response(response=200, description="Get current bonus", @SWG\Schema(ref=@Model(type=App\Entity\Bonus::class, groups={"bonusindex"}))),
* @SWG\Response(response=404, description="If there is no current bonus")
* )
*
* @Route("/current", methods={"GET"})
*
* @throws \Exception
*/
public function index(Request $request): JsonResponse
{
$bonus = $this->service->getCurrentBonus(new \DateTime());
if (!$bonus) {
return new JsonResponse([
'success' => false,
], Response::HTTP_NOT_FOUND);
}
$context = SerializationContext::create()->setGroups([
'bonusindex',
'frontendindex',
]);
$result = [
'bonus' => $bonus,
];
return JsonResponse::fromJsonString($this->serializer->serialize($result, 'json', $context));
}
}