src/Controller/v1/Notification/GetUserNotificationsController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller\v1\Notification;
  3. use App\Controller\v1\AbstractV1Controller;
  4. use App\Repository\NotificationRepository;
  5. use App\Repository\User\UserRepository;
  6. use JMS\Serializer\SerializerInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use OpenApi\Annotations as OA;
  12. /**
  13.  * @Route("/v1/user/notifications", name="user_notifications", methods={"POST"})
  14.  * @OA\Post(
  15.  *   summary="Получить уведомления пользователя",
  16.  *   description="",
  17.  *   operationId="user_notifications",
  18.  *     @OA\RequestBody(
  19.  *          required=true,
  20.  *          description="Параметры",
  21.  *          @OA\JsonContent(
  22.  *              type="object",
  23.  *              @OA\Property(
  24.  *                  property="projects",
  25.  *                  type="array",
  26.  *                  @OA\Items(type="string", example="1"),
  27.  *              ),
  28.  *              @OA\Property(property="dateFrom", type="string", example="2021-05-08"),
  29.  *              @OA\Property(property="dateTo", type="string", example="2021-08-08"),
  30.  *              @OA\Property(property="page", type="string", example=1),
  31.  *              @OA\Property(property="view", type="string", example=1),
  32.  *              @OA\Property(property="per_page", type="string", example=10),
  33.  *          ),
  34.  *     ),
  35.  * ),
  36.  * ),
  37.  * @OA\Response(
  38.  *     response=200,
  39.  *     description="Информация о пользователе получена",
  40.  * )
  41.  * @OA\Tag(name="Notification")
  42.  */
  43. class GetUserNotificationsController extends AbstractV1Controller
  44. {
  45.     private SerializerInterface $serializer;
  46.     private NotificationRepository $notificationRepository;
  47.     private UserRepository $userRepository;
  48.     public function __construct(
  49.         SerializerInterface $serializer,
  50.         NotificationRepository $notificationRepository,
  51.         UserRepository $userRepository
  52.     ) {
  53.         $this->serializer $serializer;
  54.         $this->notificationRepository $notificationRepository;
  55.         $this->userRepository $userRepository;
  56.     }
  57.     public function __invoke(
  58.         Request $request
  59.     ): JsonResponse
  60.     {
  61.         $user $this->userRepository->find($this->getUser());
  62.         if($user === null){
  63.             return $this->jsonResponse(
  64.                 null,
  65.                 Response::HTTP_INTERNAL_SERVER_ERROR,
  66.                 'Ошибка получения пользователя'
  67.             );
  68.         }
  69.         $decodedRequest $this->decodeJsonFromRequest($request);
  70.         if (empty($decodedRequest['page'])) {
  71.             $decodedRequest['page'] = 1;
  72.         }
  73.         if (empty($decodedRequest['per_page'])) {
  74.             $decodedRequest['per_page'] = 10;
  75.         }
  76.         $viewFilter $decodedRequest['view'] ?? null;
  77.         $decodedRequest['dateFrom'] = !empty($decodedRequest['dateFrom']) ? new \DateTimeImmutable($decodedRequest['dateFrom']) : null;
  78.         $decodedRequest['dateTo'] = !empty($decodedRequest['dateTo']) ? new \DateTimeImmutable($decodedRequest['dateTo']) : null;
  79.         $notifications $this->notificationRepository->findAllByUserWithLimitAndOffset(
  80.             $user,
  81.             $decodedRequest['projects'] ?? null,
  82.             $decodedRequest['dateFrom'],
  83.             $decodedRequest['dateTo'],
  84.             $viewFilter,
  85.             $decodedRequest['page'],
  86.             $decodedRequest['per_page'],
  87.             false
  88.         );
  89.         $eventInterfacePaginator = new \Doctrine\ORM\Tools\Pagination\Paginator($notifications);
  90.         $eventInterfacePaginator->setUseOutputWalkers(false);
  91.         $totalItems count($eventInterfacePaginator);
  92.         $pagesCount ceil($totalItems $decodedRequest['per_page']);
  93.         $notifications $notifications
  94.             ->setFirstResult($decodedRequest['per_page'] * ($decodedRequest['page']-1)) // set the offset
  95.             ->setMaxResults($decodedRequest['per_page'])
  96.             ->getQuery()
  97.             ->getArrayResult();
  98.         return $this->jsonResponse(
  99.             $this->collectionSerialized($this->serializer$notificationsnull, ['notification']),
  100.             Response::HTTP_OK,
  101.             'Уведомления получены',
  102.             $pagesCount
  103.         );
  104.     }
  105. }