<?php
namespace App\Controller\v1\Notification;
use App\Controller\v1\AbstractV1Controller;
use App\Repository\NotificationRepository;
use App\Repository\User\UserRepository;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use OpenApi\Annotations as OA;
/**
* @Route("/v1/user/notifications", name="user_notifications", methods={"POST"})
* @OA\Post(
* summary="Получить уведомления пользователя",
* description="",
* operationId="user_notifications",
* @OA\RequestBody(
* required=true,
* description="Параметры",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="projects",
* type="array",
* @OA\Items(type="string", example="1"),
* ),
* @OA\Property(property="dateFrom", type="string", example="2021-05-08"),
* @OA\Property(property="dateTo", type="string", example="2021-08-08"),
* @OA\Property(property="page", type="string", example=1),
* @OA\Property(property="view", type="string", example=1),
* @OA\Property(property="per_page", type="string", example=10),
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="Информация о пользователе получена",
* )
* @OA\Tag(name="Notification")
*/
class GetUserNotificationsController extends AbstractV1Controller
{
private SerializerInterface $serializer;
private NotificationRepository $notificationRepository;
private UserRepository $userRepository;
public function __construct(
SerializerInterface $serializer,
NotificationRepository $notificationRepository,
UserRepository $userRepository
) {
$this->serializer = $serializer;
$this->notificationRepository = $notificationRepository;
$this->userRepository = $userRepository;
}
public function __invoke(
Request $request
): JsonResponse
{
$user = $this->userRepository->find($this->getUser());
if($user === null){
return $this->jsonResponse(
null,
Response::HTTP_INTERNAL_SERVER_ERROR,
'Ошибка получения пользователя'
);
}
$decodedRequest = $this->decodeJsonFromRequest($request);
if (empty($decodedRequest['page'])) {
$decodedRequest['page'] = 1;
}
if (empty($decodedRequest['per_page'])) {
$decodedRequest['per_page'] = 10;
}
$viewFilter = $decodedRequest['view'] ?? null;
$decodedRequest['dateFrom'] = !empty($decodedRequest['dateFrom']) ? new \DateTimeImmutable($decodedRequest['dateFrom']) : null;
$decodedRequest['dateTo'] = !empty($decodedRequest['dateTo']) ? new \DateTimeImmutable($decodedRequest['dateTo']) : null;
$notifications = $this->notificationRepository->findAllByUserWithLimitAndOffset(
$user,
$decodedRequest['projects'] ?? null,
$decodedRequest['dateFrom'],
$decodedRequest['dateTo'],
$viewFilter,
$decodedRequest['page'],
$decodedRequest['per_page'],
false
);
$eventInterfacePaginator = new \Doctrine\ORM\Tools\Pagination\Paginator($notifications);
$eventInterfacePaginator->setUseOutputWalkers(false);
$totalItems = count($eventInterfacePaginator);
$pagesCount = ceil($totalItems / $decodedRequest['per_page']);
$notifications = $notifications
->setFirstResult($decodedRequest['per_page'] * ($decodedRequest['page']-1)) // set the offset
->setMaxResults($decodedRequest['per_page'])
->getQuery()
->getArrayResult();
return $this->jsonResponse(
$this->collectionSerialized($this->serializer, $notifications, null, ['notification']),
Response::HTTP_OK,
'Уведомления получены',
$pagesCount
);
}
}