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

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