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

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