src/Controller/Marketing/PublicBonusController.php line 51

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