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

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.     public function __construct(RateLimiter $rateLimiterNotificationService $notificationService)
  29.     {
  30.         $this->rateLimiter $rateLimiter;
  31.         $this->notificationService $notificationService;
  32.     }
  33.     /**
  34.      * @Since("6.4.7.0")
  35.      * @Route("/api/notification", name="api.notification", methods={"POST"}, defaults={"_acl"={"notification:create"}})
  36.      */
  37.     public function saveNotification(Request $requestContext $context): Response
  38.     {
  39.         $status $request->request->get('status');
  40.         $message $request->request->get('message');
  41.         $adminOnly = (bool) $request->request->get('adminOnly'false);
  42.         $requiredPrivileges $request->request->all('requiredPrivileges');
  43.         $source $context->getSource();
  44.         if (!$source instanceof AdminApiSource) {
  45.             throw new InvalidContextSourceException(AdminApiSource::class, \get_class($context->getSource()));
  46.         }
  47.         if (empty($status) || empty($message)) {
  48.             throw new \InvalidArgumentException('status and message cannot be empty');
  49.         }
  50.         if (!\is_array($requiredPrivileges)) {
  51.             throw new \InvalidArgumentException('requiredPrivileges must be an array');
  52.         }
  53.         $integrationId $source->getIntegrationId();
  54.         $createdByUserId $source->getUserId();
  55.         try {
  56.             $cacheKey $createdByUserId ?? $integrationId '-' $request->getClientIp();
  57.             $this->rateLimiter->ensureAccepted(self::NOTIFICATION$cacheKey);
  58.         } catch (RateLimitExceededException $exception) {
  59.             throw new NotificationThrottledException($exception->getWaitTime(), $exception);
  60.         }
  61.         $notificationId Uuid::randomHex();
  62.         $this->notificationService->createNotification([
  63.             'id' => $notificationId,
  64.             'status' => $status,
  65.             'message' => $message,
  66.             'adminOnly' => $adminOnly,
  67.             'requiredPrivileges' => $requiredPrivileges,
  68.             'createdByIntegrationId' => $integrationId,
  69.             'createdByUserId' => $createdByUserId,
  70.         ], $context);
  71.         return new JsonResponse(['id' => $notificationId]);
  72.     }
  73.     /**
  74.      * @Since("6.4.7.0")
  75.      * @Route("/api/notification/message", name="api.notification.message", methods={"GET"})
  76.      */
  77.     public function fetchNotification(Request $requestContext $context): Response
  78.     {
  79.         $limit $request->query->get('limit');
  80.         $limit $limit ? (int) $limit self::LIMIT;
  81.         $latestTimestamp $request->query->has('latestTimestamp') ? (string) $request->query->get('latestTimestamp') : null;
  82.         $responseData $this->notificationService->getNotifications($context$limit$latestTimestamp);
  83.         return new JsonResponse($responseData);
  84.     }
  85. }