forked from shopware/shopware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachineActionController.php
126 lines (113 loc) · 4.86 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
121
122
123
124
125
126
<?php declare(strict_types=1);
namespace Shopware\Core\System\StateMachine\Api;
use Shopware\Core\Framework\Api\Acl\Role\AclRoleDefinition;
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\Log\Package;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateDefinition;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionEntity;
use Shopware\Core\System\StateMachine\StateMachineException;
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\Attribute\Route;
#[Route(defaults: ['_routeScope' => ['api']])]
#[Package('checkout')]
class StateMachineActionController extends AbstractController
{
/**
* @var StateMachineRegistry
*
* @deprecated tag:v6.7.0 - will be private and typed
*/
protected $stateMachineRegistry;
/**
* @internal
*/
public function __construct(
StateMachineRegistry $stateMachineRegistry,
private readonly DefinitionInstanceRegistry $definitionInstanceRegistry
) {
$this->stateMachineRegistry = $stateMachineRegistry;
}
#[Route(path: '/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 {
$this->validatePrivilege($entityName, AclRoleDefinition::PRIVILEGE_READ, $context);
$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,
]);
}
#[Route(path: '/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 {
$this->validatePrivilege($entityName, AclRoleDefinition::PRIVILEGE_UPDATE, $context);
$stateFieldName = (string) $request->query->get('stateFieldName', 'stateId');
$stateMachineStateCollection = $this->stateMachineRegistry->transition(
new Transition(
$entityName,
$entityId,
$transition,
$stateFieldName
),
$context
);
$toPlace = $stateMachineStateCollection->get('toPlace');
if ($toPlace === null) {
throw StateMachineException::stateMachineStateNotFound($entityName, $transition);
}
return $responseFactory->createDetailResponse(
new Criteria(),
$toPlace,
$this->definitionInstanceRegistry->get(StateMachineStateDefinition::class),
$request,
$context
);
}
private function validatePrivilege(string $entityName, string $privilege, Context $context): void
{
$permission = $entityName . ':' . $privilege;
if (!$context->isAllowed($permission)) {
throw StateMachineException::missingPrivileges([$permission]);
}
}
}