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