-
Notifications
You must be signed in to change notification settings - Fork 12
/
WorkflowControllerTrait.php
165 lines (144 loc) · 5.62 KB
/
WorkflowControllerTrait.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Yokai\SonataWorkflow\Controller;
use Psr\Container\ContainerInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Exception\LockException;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
use Symfony\Component\Workflow\Exception\LogicException;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Workflow\WorkflowInterface;
/**
*
* @author Yann Eugoné <[email protected]>
*/
trait WorkflowControllerTrait
{
private Registry $workflowRegistry;
/**
* @required Symfony DI autowiring
*/
public function setWorkflowRegistry(Registry $workflowRegistry): void
{
$this->workflowRegistry = $workflowRegistry;
}
public function workflowApplyTransitionAction(Request $request): Response
{
$id = $request->get($this->admin->getIdParameter());
$existingObject = $this->admin->getObject($id);
if (!$existingObject) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->admin->setSubject($existingObject);
$this->admin->checkAccess('applyTransitions', $existingObject);
$objectId = $this->admin->getNormalizedIdentifier($existingObject);
try {
$workflow = $this->getWorkflow($existingObject);
} catch (InvalidArgumentException $exception) {
throw $this->createNotFoundException('Not found', $exception);
}
$transition = $request->get('transition', null);
if ($transition === null) {
throw new BadRequestHttpException('missing transition to apply');
}
if (!$workflow->can($existingObject, $transition)) {
throw new BadRequestHttpException(
sprintf(
'transition %s could not be applied to object %s',
$transition,
$this->admin->toString($existingObject)
)
);
}
$response = $this->preApplyTransition($existingObject, $transition);
if ($response !== null) {
return $response;
}
try {
$workflow->apply($existingObject, $transition);
$existingObject = $this->admin->update($existingObject);
if ($this->isXmlHttpRequest($request)) {
return $this->renderJson(
[
'result' => 'ok',
'objectId' => $objectId,
'objectName' => $this->escapeHtml($this->admin->toString($existingObject)),
],
200,
[]
);
}
$this->addFlash(
'sonata_flash_success',
$this->trans(
'flash_edit_success',
['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
'SonataAdminBundle'
)
);
} catch (LogicException $e) {
throw new BadRequestHttpException(
sprintf(
'transition %s could not be applied to object %s',
$transition,
$this->admin->toString($existingObject)
),
$e
);
} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);
} catch (LockException $e) {
$this->addFlash(
'sonata_flash_error',
$this->trans(
'flash_lock_error',
[
'%name%' => $this->escapeHtml($this->admin->toString($existingObject)),
'%link_start%' => '<a href="' . $this->admin->generateObjectUrl('edit', $existingObject) . '">',
'%link_end%' => '</a>',
],
'SonataAdminBundle'
)
);
}
return $this->redirectTo($request, $existingObject);
}
/**
* @throws InvalidArgumentException
*/
protected function getWorkflow(object $object): WorkflowInterface
{
$registry = $this->workflowRegistry ?? null;
if ($registry === null) {
try {
if (method_exists($this, 'get')) {
$registry = $this->get('workflow.registry');
} elseif (method_exists($this, 'getContainer')) {
$registry = $this->getContainer()->get('workflow.registry');
} else {
$registry = $this->container->get('workflow.registry');
}
} catch (ServiceNotFoundException $exception) {
throw new \LogicException(
'Could not find the "workflow.registry" service. ' .
'You should either provide it via setter injection in your controller service definition ' .
'or make it public in your project.',
0,
$exception
);
}
}
return $registry->get($object);
}
protected function preApplyTransition(object $object, string $transition): ?Response
{
return null;
}
}