src/Controller/Controlpanel/Api/NotificationsController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Controlpanel\Api;
  3. use App\Application\DTO\ControlPanel\Api\JsonApi\JsonApiDocument;
  4. use App\Application\DTO\ControlPanel\Api\QueryParameter\NotificationQueryParameter;
  5. use App\Application\Service\ControlPanel\Api\Notification\MarkAsReadNotificationsService;
  6. use App\Application\Service\ControlPanel\Api\Notification\NotificationConfigurationService;
  7. use App\Application\Service\ControlPanel\Api\Notification\NotificationJsonApiPatcherService;
  8. use App\Application\Service\ControlPanel\Api\Notification\NotificationJsonApiService;
  9. use App\Application\Service\Session\SessionService;
  10. use App\Entity\System\Customer;
  11. use App\Exception\ControlPanel\ControlPanelNotFoundException;
  12. use App\Model\Controlpanel\Response\JsonApiResponse;
  13. use App\Model\Controlpanel\Response\WebJsonApiResponse;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. /**
  18. * @Route("/controlpanel/api/notifications", name="api_controlpanel_notification_")
  19. */
  20. class NotificationsController extends AbstractController
  21. {
  22. public function __construct(
  23. private readonly NotificationConfigurationService $notificationService,
  24. private readonly MarkAsReadNotificationsService $markAsReadNotificationsService,
  25. private readonly SessionService $sessionService,
  26. ) {
  27. }
  28. /**
  29. * @Route("/config", name="config_list", methods={"GET"})
  30. *
  31. * @return JsonApiResponse
  32. */
  33. public function getNotificationsConfiguration(): JsonApiResponse
  34. {
  35. /** @var Customer $customer */
  36. $customer = $this->getUser();
  37. $jsonApiDocument = new JsonApiDocument();
  38. $jsonApiDocument->data = $this->notificationService->getNotifications($customer);
  39. return new JsonApiResponse($jsonApiDocument);
  40. }
  41. /**
  42. * @param Request $request
  43. *
  44. * @return JsonApiResponse
  45. *
  46. * @Route("/config", name="config_update", methods={"PATCH"})
  47. */
  48. public function updateConfiguration(Request $request): JsonApiResponse
  49. {
  50. /** @var Customer $customer */
  51. $customer = $this->getUser();
  52. $requestDocument = $request->attributes->get('json_api_document');
  53. $newsletterActive = (bool)$requestDocument->data->attributes->newsletter;
  54. $jsonApiDocument = new JsonApiDocument();
  55. $jsonApiDocument->data = $this->notificationService->updateNotification($customer, $newsletterActive, $request->getClientIp());
  56. return new JsonApiResponse($jsonApiDocument);
  57. }
  58. /**
  59. * @param Request $request
  60. * @param NotificationJsonApiService $notificationJsonApiService
  61. *
  62. * @return WebJsonApiResponse
  63. *
  64. * @throws \Exception
  65. *
  66. * @Route("", name="list", methods={"GET"})
  67. */
  68. public function list(Request $request, NotificationJsonApiService $notificationJsonApiService): WebJsonApiResponse
  69. {
  70. /** @var Customer $customer */
  71. $customer = $this->getUser();
  72. $parameters = new NotificationQueryParameter(
  73. $request->query->all(),
  74. $customer->getId(),
  75. $this->sessionService->getLocaleId(),
  76. $this->sessionService->getLocale()
  77. );
  78. $document = $notificationJsonApiService->getCollectionDocument($parameters);
  79. return new WebJsonApiResponse($document);
  80. }
  81. /**
  82. * @param int $customerNotificationId
  83. * @param Request $request
  84. * @param NotificationJsonApiPatcherService $service
  85. *
  86. * @return WebJsonApiResponse
  87. *
  88. * @throws ControlPanelNotFoundException
  89. *
  90. * @Route("/{customerNotificationId<\d+>}", name="update", methods={"PATCH"})
  91. */
  92. public function patch(int $customerNotificationId, Request $request, NotificationJsonApiPatcherService $service): WebJsonApiResponse
  93. {
  94. /** @var Customer $customer */
  95. $customer = $this->getUser();
  96. /** @var \App\Application\DTO\JsonApi\JsonApiDocument $document */
  97. $document = $request->attributes->get('json_api_document');
  98. $updatedDocument = $service->patchDocument(
  99. $customerNotificationId,
  100. $document,
  101. $customer->getId(),
  102. $this->sessionService->getLocaleId()
  103. );
  104. return new WebJsonApiResponse($updatedDocument);
  105. }
  106. /**
  107. * @param Request $request
  108. *
  109. * @return WebJsonApiResponse
  110. *
  111. * @throws \Exception
  112. *
  113. * @Route("/mark-all-as-read", name="mark_all_as_read ", methods={"PATCH"})
  114. */
  115. public function allRead(Request $request): WebJsonApiResponse
  116. {
  117. /** @var Customer $customer */
  118. $customer = $this->getUser();
  119. $updatedNotificationsDocument = $this->markAsReadNotificationsService->markNotificationsAsRead(
  120. $customer->getId(),
  121. $this->sessionService->getLocaleId()
  122. );
  123. return new WebJsonApiResponse($updatedNotificationsDocument);
  124. }
  125. }