vendor/shopware/administration/Controller/NotificationController.php line 98

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Administration\Controller;
  3. use Shopware\Administration\Notification\Exception\NotificationThrottledException;
  4. use Shopware\Administration\Notification\NotificationService;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\Exception\InvalidContextSourceException;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\RateLimiter\Exception\RateLimitExceededException;
  10. use Shopware\Core\Framework\RateLimiter\RateLimiter;
  11. use Shopware\Core\Framework\Routing\Annotation\Acl;
  12. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  13. use Shopware\Core\Framework\Routing\Annotation\Since;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * @Route(defaults={"_routeScope"={"api"}})
  22.  */
  23. #[Package('administration')]
  24. class NotificationController extends AbstractController
  25. {
  26.     public const NOTIFICATION 'notification';
  27.     public const LIMIT 5;
  28.     private RateLimiter $rateLimiter;
  29.     private NotificationService $notificationService;
  30.     /**
  31.      * @internal
  32.      */
  33.     public function __construct(RateLimiter $rateLimiterNotificationService $notificationService)
  34.     {
  35.         $this->rateLimiter $rateLimiter;
  36.         $this->notificationService $notificationService;
  37.     }
  38.     /**
  39.      * @Since("6.4.7.0")
  40.      * @Route("/api/notification", name="api.notification", methods={"POST"}, defaults={"_acl"={"notification:create"}})
  41.      */
  42.     public function saveNotification(Request $requestContext $context): Response
  43.     {
  44.         $status $request->request->get('status');
  45.         $message $request->request->get('message');
  46.         $adminOnly = (bool) $request->request->get('adminOnly'false);
  47.         $requiredPrivileges $request->request->all('requiredPrivileges');
  48.         $source $context->getSource();
  49.         if (!$source instanceof AdminApiSource) {
  50.             throw new InvalidContextSourceException(AdminApiSource::class, \get_class($context->getSource()));
  51.         }
  52.         if (empty($status) || empty($message)) {
  53.             throw new \InvalidArgumentException('status and message cannot be empty');
  54.         }
  55.         if (!\is_array($requiredPrivileges)) {
  56.             throw new \InvalidArgumentException('requiredPrivileges must be an array');
  57.         }
  58.         $integrationId $source->getIntegrationId();
  59.         $createdByUserId $source->getUserId();
  60.         try {
  61.             $cacheKey $createdByUserId ?? $integrationId '-' $request->getClientIp();
  62.             $this->rateLimiter->ensureAccepted(self::NOTIFICATION$cacheKey);
  63.         } catch (RateLimitExceededException $exception) {
  64.             throw new NotificationThrottledException($exception->getWaitTime(), $exception);
  65.         }
  66.         $notificationId Uuid::randomHex();
  67.         $this->notificationService->createNotification([
  68.             'id' => $notificationId,
  69.             'status' => $status,
  70.             'message' => $message,
  71.             'adminOnly' => $adminOnly,
  72.             'requiredPrivileges' => $requiredPrivileges,
  73.             'createdByIntegrationId' => $integrationId,
  74.             'createdByUserId' => $createdByUserId,
  75.         ], $context);
  76.         return new JsonResponse(['id' => $notificationId]);
  77.     }
  78.     /**
  79.      * @Since("6.4.7.0")
  80.      * @Route("/api/notification/message", name="api.notification.message", methods={"GET"})
  81.      */
  82.     public function fetchNotification(Request $requestContext $context): Response
  83.     {
  84.         $limit $request->query->get('limit');
  85.         $limit $limit ? (int) $limit self::LIMIT;
  86.         $latestTimestamp $request->query->has('latestTimestamp') ? (string) $request->query->get('latestTimestamp') : null;
  87.         $responseData $this->notificationService->getNotifications($context$limit$latestTimestamp);
  88.         return new JsonResponse($responseData);
  89.     }
  90. }