vendor/shopware/core/System/StateMachine/Api/StateMachineActionController.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\StateMachine\Api;
  3. use Shopware\Core\Framework\Api\Response\ResponseFactoryInterface;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Framework\Routing\Annotation\Since;
  10. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateDefinition;
  11. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionEntity;
  12. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  13. use Shopware\Core\System\StateMachine\Transition;
  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. #[Package('checkout')]
  23. class StateMachineActionController extends AbstractController
  24. {
  25.     /**
  26.      * @var StateMachineRegistry
  27.      */
  28.     protected $stateMachineRegistry;
  29.     /**
  30.      * @var DefinitionInstanceRegistry
  31.      */
  32.     private $definitionInstanceRegistry;
  33.     /**
  34.      * @internal
  35.      */
  36.     public function __construct(
  37.         StateMachineRegistry $stateMachineRegistry,
  38.         DefinitionInstanceRegistry $definitionInstanceRegistry
  39.     ) {
  40.         $this->stateMachineRegistry $stateMachineRegistry;
  41.         $this->definitionInstanceRegistry $definitionInstanceRegistry;
  42.     }
  43.     /**
  44.      * @Since("6.0.0.0")
  45.      * @Route("/api/_action/state-machine/{entityName}/{entityId}/state", name="api.state_machine.states", methods={"GET"})
  46.      */
  47.     public function getAvailableTransitions(
  48.         Request $request,
  49.         Context $context,
  50.         string $entityName,
  51.         string $entityId
  52.     ): JsonResponse {
  53.         $stateFieldName = (string) $request->query->get('stateFieldName''stateId');
  54.         $availableTransitions $this->stateMachineRegistry->getAvailableTransitions(
  55.             $entityName,
  56.             $entityId,
  57.             $stateFieldName,
  58.             $context
  59.         );
  60.         $transitionsJson = [];
  61.         /** @var StateMachineTransitionEntity $transition */
  62.         foreach ($availableTransitions as $transition) {
  63.             $transitionsJson[] = [
  64.                 'name' => $transition->getToStateMachineState()->getName(),
  65.                 'technicalName' => $transition->getToStateMachineState()->getTechnicalName(),
  66.                 'actionName' => $transition->getActionName(),
  67.                 'fromStateName' => $transition->getFromStateMachineState()->getTechnicalName(),
  68.                 'toStateName' => $transition->getToStateMachineState()->getTechnicalName(),
  69.                 'url' => $this->generateUrl('api.state_machine.transition_state', [
  70.                     'entityName' => $entityName,
  71.                     'entityId' => $entityId,
  72.                     'version' => $request->attributes->get('version'),
  73.                     'transition' => $transition->getActionName(),
  74.                 ]),
  75.             ];
  76.         }
  77.         return new JsonResponse([
  78.             'transitions' => $transitionsJson,
  79.         ]);
  80.     }
  81.     /**
  82.      * @Since("6.0.0.0")
  83.      * @Route("/api/_action/state-machine/{entityName}/{entityId}/state/{transition}", name="api.state_machine.transition_state", methods={"POST"})
  84.      */
  85.     public function transitionState(
  86.         Request $request,
  87.         Context $context,
  88.         ResponseFactoryInterface $responseFactory,
  89.         string $entityName,
  90.         string $entityId,
  91.         string $transition
  92.     ): Response {
  93.         $stateFieldName = (string) $request->query->get('stateFieldName''stateId');
  94.         $stateMachineStateCollection $this->stateMachineRegistry->transition(
  95.             new Transition(
  96.                 $entityName,
  97.                 $entityId,
  98.                 $transition,
  99.                 $stateFieldName
  100.             ),
  101.             $context
  102.         );
  103.         return $responseFactory->createDetailResponse(
  104.             new Criteria(),
  105.             $stateMachineStateCollection->get('toPlace'),
  106.             $this->definitionInstanceRegistry->get(StateMachineStateDefinition::class),
  107.             $request,
  108.             $context
  109.         );
  110.     }
  111. }