src/Controller/Marketing/PublicMarketingImageController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Marketing;
  3. use App\Service\Marketing\MarketingImageService;
  4. use JMS\Serializer\SerializationContext;
  5. use JMS\Serializer\SerializerInterface;
  6. use Nelmio\ApiDocBundle\Annotation\Model;
  7. use Swagger\Annotations as SWG;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/api/public/marketing/image")
  15.  */
  16. class PublicMarketingImageController extends AbstractController
  17. {
  18.     /**
  19.      * PublicMarketingImageController constructor.
  20.      */
  21.     public function __construct(private MarketingImageService $service, private SerializerInterface $serializer)
  22.     {
  23.     }
  24.     /**
  25.      * @SWG\Get(tags={"Bonus"},
  26.      *
  27.      *    @SWG\Response(response=200, description="Get current marketing image", @SWG\Schema(ref=@Model(type=App\Entity\BonusImage::class, groups={"bonusindex"}))),
  28.      *    @SWG\Response(response=404, description="If there is no current marketing image")
  29.      * )
  30.      *
  31.      * @Route("/current", methods={"GET"})
  32.      *
  33.      * @throws \Exception
  34.      */
  35.     public function index(Request $request): JsonResponse
  36.     {
  37.         $marketingImage $this->service->getCurrentMarketingImage(new \DateTime());
  38.         if (!$marketingImage) {
  39.             return new JsonResponse([
  40.                 'success' => false,
  41.             ], Response::HTTP_NOT_FOUND);
  42.         }
  43.         $context SerializationContext::create()->setGroups([
  44.             'bonusindex',
  45.             'frontendindex',
  46.         ]);
  47.         $result = [
  48.             'marketingImage' => $marketingImage,
  49.         ];
  50.         return JsonResponse::fromJsonString($this->serializer->serialize($result'json'$context));
  51.     }
  52. }