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

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