vendor/shopware/core/Framework/Increment/Controller/IncrementApiController.php line 74

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Increment\Controller;
  3. use Shopware\Core\Framework\Increment\IncrementGatewayRegistry;
  4. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  5. use Shopware\Core\Framework\Routing\Annotation\Since;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. /**
  11.  * @RouteScope(scopes={"api"})
  12.  */
  13. class IncrementApiController
  14. {
  15.     private IncrementGatewayRegistry $gatewayRegistry;
  16.     public function __construct(IncrementGatewayRegistry $gatewayRegistry)
  17.     {
  18.         $this->gatewayRegistry $gatewayRegistry;
  19.     }
  20.     /**
  21.      * @Since("6.4.6.0")
  22.      * @Route("/api/_action/increment/{pool}", name="api.increment.increment", methods={"POST"})
  23.      */
  24.     public function increment(Request $requeststring $pool): Response
  25.     {
  26.         $key $request->request->get('key');
  27.         if (!$key || !\is_string($key)) {
  28.             throw new \InvalidArgumentException('Increment key must be null or a string');
  29.         }
  30.         $cluster $this->getCluster($request);
  31.         $poolGateway $this->gatewayRegistry->get($pool);
  32.         $poolGateway->increment($cluster$key);
  33.         return new JsonResponse(['success' => true]);
  34.     }
  35.     /**
  36.      * @Since("6.4.6.0")
  37.      * @Route("/api/_action/decrement/{pool}", name="api.increment.decrement", methods={"POST"})
  38.      */
  39.     public function decrement(Request $requeststring $pool): Response
  40.     {
  41.         $key $request->request->get('key');
  42.         if (!$key || !\is_string($key)) {
  43.             throw new \InvalidArgumentException('Increment key must be null or a string');
  44.         }
  45.         $cluster $this->getCluster($request);
  46.         $poolGateway $this->gatewayRegistry->get($pool);
  47.         $poolGateway->decrement(
  48.             $cluster,
  49.             $key
  50.         );
  51.         return new JsonResponse(['success' => true]);
  52.     }
  53.     /**
  54.      * @Since("6.4.6.0")
  55.      * @Route("/api/_action/increment/{pool}", name="api.increment.list", methods={"GET"})
  56.      */
  57.     public function getIncrement(string $poolRequest $request): Response
  58.     {
  59.         $cluster $this->getCluster($request);
  60.         $poolGateway $this->gatewayRegistry->get($pool);
  61.         $limit $request->query->getInt('limit'5);
  62.         $offset $request->query->getInt('offset'0);
  63.         $result $poolGateway->list($cluster$limit$offset);
  64.         return new JsonResponse($result);
  65.     }
  66.     /**
  67.      * @Since("6.4.6.0")
  68.      * @Route("/api/_action/reset-increment/{pool}", name="api.increment.reset", methods={"POST"})
  69.      */
  70.     public function reset(string $poolRequest $request): Response
  71.     {
  72.         $cluster $this->getCluster($request);
  73.         $poolGateway $this->gatewayRegistry->get($pool);
  74.         $key $request->request->get('key');
  75.         if ($key !== null && !\is_string($key)) {
  76.             throw new \InvalidArgumentException('Increment key must be null or a string');
  77.         }
  78.         $poolGateway->reset($cluster$key);
  79.         return new JsonResponse(['success' => true]);
  80.     }
  81.     private function getCluster(Request $request): string
  82.     {
  83.         $cluster $request->get('cluster');
  84.         if ($cluster && \is_string($cluster)) {
  85.             return $cluster;
  86.         }
  87.         throw new \InvalidArgumentException('Argument cluster is missing or invalid');
  88.     }
  89. }