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

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