<?php
namespace App\Controller\Controlpanel\Api;
use App\Application\DTO\ControlPanel\Api\JsonApi\JsonApiDocument;
use App\Application\DTO\ControlPanel\Api\QueryParameter\NotificationQueryParameter;
use App\Application\Service\ControlPanel\Api\Notification\MarkAsReadNotificationsService;
use App\Application\Service\ControlPanel\Api\Notification\NotificationConfigurationService;
use App\Application\Service\ControlPanel\Api\Notification\NotificationJsonApiPatcherService;
use App\Application\Service\ControlPanel\Api\Notification\NotificationJsonApiService;
use App\Entity\System\Customer;
use App\Entity\System\Language;
use App\Exception\ControlPanel\ControlPanelNotFoundException;
use App\Manager\System\LanguageManager;
use App\Model\Controlpanel\Response\JsonApiResponse;
use App\Model\Controlpanel\Response\WebJsonApiResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/controlpanel/api/notifications", name="api_controlpanel_notification_")
*/
class NotificationsController extends AbstractController
{
private NotificationConfigurationService $notificationService;
private MarkAsReadNotificationsService $markAsReadNotificationsService;
private LanguageManager $languageManager;
public function __construct(
NotificationConfigurationService $notificationService,
MarkAsReadNotificationsService $markAsReadNotificationsService,
LanguageManager $languageManager
) {
$this->notificationService = $notificationService;
$this->markAsReadNotificationsService = $markAsReadNotificationsService;
$this->languageManager = $languageManager;
}
/**
* @Route("/config", name="config_list", methods={"GET"})
*
* @return JsonApiResponse
*/
public function getNotificationsConfiguration(): JsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
$jsonApiDocument = new JsonApiDocument();
$jsonApiDocument->data = $this->notificationService->getNotifications($customer);
return new JsonApiResponse($jsonApiDocument);
}
/**
* @param Request $request
*
* @return JsonApiResponse
*
* @Route("/config", name="config_update", methods={"PATCH"})
*/
public function updateConfiguration(Request $request): JsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
$requestDocument = $request->attributes->get('json_api_document');
$newsletterActive = (bool)$requestDocument->data->attributes->newsletter;
$jsonApiDocument = new JsonApiDocument();
$jsonApiDocument->data = $this->notificationService->updateNotification($customer, $newsletterActive, $request->getClientIp());
return new JsonApiResponse($jsonApiDocument);
}
/**
* @param Request $request
* @param NotificationJsonApiService $notificationJsonApiService
*
* @return WebJsonApiResponse
*
* @throws \Exception
*
* @Route("", name="list", methods={"GET"})
*/
public function list(Request $request, NotificationJsonApiService $notificationJsonApiService): WebJsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
/** @var Language $language */
$language = $this->languageManager->findOneByIsoCode($request->getSession()->get('lang'));
$parameters = new NotificationQueryParameter($request->query->all(), $customer->getId(), $language->getId(), $language->getIsoCode());
$document = $notificationJsonApiService->getCollectionDocument($parameters);
return new WebJsonApiResponse($document);
}
/**
* @param int $customerNotificationId
* @param Request $request
* @param NotificationJsonApiPatcherService $service
*
* @return WebJsonApiResponse
*
* @throws ControlPanelNotFoundException
*
* @Route("/{customerNotificationId<\d+>}", name="update", methods={"PATCH"})
*/
public function patch(int $customerNotificationId, Request $request, NotificationJsonApiPatcherService $service): WebJsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
/** @var \App\Application\DTO\JsonApi\JsonApiDocument $document */
$document = $request->attributes->get('json_api_document');
/** @var Language $language */
$language = $this->languageManager->findOneByIsoCode($request->getSession()->get('lang'));
$updatedDocument = $service->patchDocument($customerNotificationId, $document, $customer->getId(), $language->getId());
return new WebJsonApiResponse($updatedDocument);
}
/**
* @param Request $request
*
* @return WebJsonApiResponse
*
* @throws \Exception
*
* @Route("/mark-all-as-read", name="mark_all_as_read ", methods={"PATCH"})
*/
public function allRead(Request $request): WebJsonApiResponse
{
/** @var Customer $customer */
$customer = $this->getUser();
/** @var Language $language */
$language = $this->languageManager->findOneByIsoCode($request->getSession()->get('lang'));
$updatedNotificationsDocument = $this->markAsReadNotificationsService->markNotificationsAsRead($customer->getId(), $language->getId());
return new WebJsonApiResponse($updatedNotificationsDocument);
}
}