-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathFunnelWorker.php
More file actions
283 lines (228 loc) · 11.2 KB
/
Copy pathFunnelWorker.php
File metadata and controls
283 lines (228 loc) · 11.2 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - DACHCOM Commercial License (DCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) DACHCOM.DIGITAL AG (https://www.dachcom-digital.com)
* @license GPLv3 and DCL
*/
namespace FormBuilderBundle\OutputWorkflow;
use FormBuilderBundle\Assembler\FunnelActionElementAssembler;
use FormBuilderBundle\Builder\FrontendFormBuilder;
use FormBuilderBundle\Configuration\Configuration;
use FormBuilderBundle\Event\SubmissionEvent;
use FormBuilderBundle\Form\Data\FormDataInterface;
use FormBuilderBundle\Form\FormValuesInputApplierInterface;
use FormBuilderBundle\Model\FormStorageData;
use FormBuilderBundle\Model\FunnelActionElement;
use FormBuilderBundle\Model\OutputWorkflowChannelInterface;
use FormBuilderBundle\Model\OutputWorkflowInterface;
use FormBuilderBundle\OutputWorkflow\Channel\ChannelContext;
use FormBuilderBundle\OutputWorkflow\Channel\ChannelContextAwareInterface;
use FormBuilderBundle\OutputWorkflow\Channel\ChannelResponseAwareInterface;
use FormBuilderBundle\OutputWorkflow\Channel\Funnel\Action\FunnelActionElementStack;
use FormBuilderBundle\OutputWorkflow\Channel\FunnelAwareChannelInterface;
use FormBuilderBundle\Registry\OutputWorkflowChannelRegistry;
use FormBuilderBundle\Registry\StorageProviderRegistry;
use FormBuilderBundle\Resolver\FunnelDataResolver;
use FormBuilderBundle\Storage\StorageProviderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FunnelWorker implements FunnelWorkerInterface
{
public function __construct(
protected Configuration $configuration,
protected FormValuesInputApplierInterface $formValuesInputApplier,
protected StorageProviderRegistry $storageProviderRegistry,
protected OutputWorkflowChannelRegistry $channelRegistry,
protected FrontendFormBuilder $frontendFormBuilder,
protected FunnelActionElementAssembler $funnelActionElementAssembler,
protected FunnelDataResolver $funnelDataResolver,
) {
}
/**
* @throws \Exception
*/
public function initiateFunnel(OutputWorkflowInterface $outputWorkflow, SubmissionEvent $submissionEvent): void
{
if (!$outputWorkflow->isFunnelWorkflow()) {
throw new \Exception(sprintf('Funnel with id %d is not a valid funnel', $outputWorkflow->getId()));
}
// get first channel
$channels = $outputWorkflow->getChannels();
if (count($channels) === 0) {
throw new \Exception(sprintf('Funnel with id %d does not provide any channels', $outputWorkflow->getId()));
}
/** @var FormDataInterface $formData */
$formData = $submissionEvent->getForm()->getData();
$initiationPath = $submissionEvent->getRequest()->getPathInfo();
if ($submissionEvent->getRequest()->isXmlHttpRequest()) {
$startOffUrlInfo = parse_url($submissionEvent->getRequest()->headers->get('referer', ''));
$initiationPath = $startOffUrlInfo['path'] ?? null;
}
$formStorageData = new FormStorageData(
$formData->getFormDefinition()->getId(),
$formData->getData(),
$submissionEvent->getFormRuntimeData(),
$initiationPath
);
$storageProvider = $this->getStorageProvider();
$storageToken = $storageProvider->store($submissionEvent->getRequest(), $formStorageData);
/** @var OutputWorkflowChannelInterface $firstChannel */
$firstChannel = $channels[0];
$channelProcessor = $this->channelRegistry->get($firstChannel->getType());
$funnelActionElement = $this->funnelActionElementAssembler->assembleItem(
$firstChannel,
$channelProcessor,
[
'type' => 'channelAction',
'triggerName' => '__INITIATE_FUNNEL',
'configuration' => [
'channelName' => $firstChannel->getName()
]
],
[
'storageToken' => $storageToken,
'initiationPath' => $formStorageData->getInitiationPath()
]
);
$submissionEvent->setRedirectUri($funnelActionElement->getPath());
}
/**
* @throws \Exception
*/
public function processFunnel(OutputWorkflowInterface $outputWorkflow, Request $request): Response
{
$funnelData = $this->funnelDataResolver->getFunnelData($request);
if (!$outputWorkflow->isFunnelWorkflow()) {
throw new \Exception(sprintf('Funnel with id %d is not a valid funnel', $outputWorkflow->getId()));
}
$storageToken = $funnelData->getStorageToken();
$channel = $outputWorkflow->getChannelByName($funnelData->getChannelId());
if (!$channel instanceof OutputWorkflowChannelInterface) {
throw new \Exception(sprintf('Channel with id %d not found', $funnelData->getChannelId()));
}
$formStorageData = $funnelData->getFormStorageData();
// restore submission event to allow seamless output workflow channel processing
$submissionEvent = $this->buildSubmissionEvent($request, $outputWorkflow, $formStorageData);
$channelProcessor = $this->channelRegistry->get($channel->getType());
$funnelWorkerData = new FunnelWorkerData(
$funnelData,
$submissionEvent,
$outputWorkflow,
$channel,
$channelProcessor
);
// it's a layout funnel
if ($channelProcessor instanceof FunnelAwareChannelInterface) {
$funnelWorkerData->setFunnelActionElementStack($this->buildFunnelActionElementStack($funnelWorkerData));
$funnelResponse = $channelProcessor->dispatchFunnelProcessing($funnelWorkerData);
if ($formStorageData->funnelRuntimeDataDirty()) {
$this->getStorageProvider()->update($request, $storageToken, $formStorageData);
$formStorageData->resetRuntimeDataDirty();
}
return $funnelResponse;
}
// it's a virtual funnel, process channel
return $this->processDataFunnel($funnelWorkerData);
}
protected function processDataFunnel(FunnelWorkerData $funnelWorkerData): Response
{
$dataChannelResponse = null;
$channelProcessor = $funnelWorkerData->getChannelProcessor();
$channelContext = new ChannelContext();
try {
$arguments = [
$funnelWorkerData->getSubmissionEvent(),
$funnelWorkerData->getOutputWorkflow()->getName(),
$funnelWorkerData->getChannel()->getConfiguration()
];
if ($channelProcessor instanceof ChannelContextAwareInterface) {
$channelProcessor->setChannelContext($channelContext);
}
if ($channelProcessor instanceof ChannelResponseAwareInterface) {
$dataChannelResponse = $channelProcessor->dispatchResponseOutputProcessing(...$arguments);
} else {
$channelProcessor->dispatchOutputProcessing(...$arguments);
}
} catch (\Throwable $e) {
$funnelErrorToken = $funnelWorkerData->getFormStorageData()->addFunnelError($e->getMessage());
$this->getStorageProvider()->update(
$funnelWorkerData->getRequest(),
$funnelWorkerData->getStorageToken(),
$funnelWorkerData->getFormStorageData()
);
$funnelWorkerData->setFunnelActionElementStack($this->buildFunnelActionElementStack($funnelWorkerData, $funnelErrorToken));
$virtualFunnelError = $funnelWorkerData->getFunnelActionElementStack()->getByName('virtualFunnelError');
if (!$virtualFunnelError instanceof FunnelActionElement) {
throw new \Exception('Could not resolve virtual error funnel');
}
return new RedirectResponse($virtualFunnelError->getPath());
}
if ($dataChannelResponse instanceof Response) {
return $dataChannelResponse;
}
$funnelWorkerData->setFunnelActionElementStack($this->buildFunnelActionElementStack($funnelWorkerData));
$virtualFunnelSuccess = $funnelWorkerData->getFunnelActionElementStack()->getByName('virtualFunnelSuccess');
if (!$virtualFunnelSuccess instanceof FunnelActionElement) {
throw new \Exception('Could not resolve virtual success funnel');
}
if ($virtualFunnelSuccess->isChannelAware()) {
/** @var OutputWorkflowChannelInterface $nextVirtualChannel */
$nextVirtualChannel = $virtualFunnelSuccess->getSubject();
$nextVirtualChannelProcessor = $this->channelRegistry->get($nextVirtualChannel->getType());
// instance dispatch is allowed for virtual funnels only!
if ($nextVirtualChannelProcessor instanceof FunnelAwareChannelInterface) {
return new RedirectResponse($virtualFunnelSuccess->getPath());
}
$nextVirtualFunnelWorkerData = new FunnelWorkerData(
$funnelWorkerData->getFunnelData(),
$funnelWorkerData->getSubmissionEvent(),
$funnelWorkerData->getOutputWorkflow(),
$nextVirtualChannel,
$nextVirtualChannelProcessor
);
return $this->processDataFunnel($nextVirtualFunnelWorkerData);
}
return new RedirectResponse($virtualFunnelSuccess->getPath());
}
protected function buildFunnelActionElementStack(FunnelWorkerData $funnelWorkerData, ?string $funnelErrorToken = null): FunnelActionElementStack
{
$context = [
'storageToken' => $funnelWorkerData->getStorageToken(),
'initiationPath' => $funnelWorkerData->getFormStorageData()->getInitiationPath()
];
if ($funnelErrorToken !== null) {
$context['errorToken'] = $funnelErrorToken;
}
return $this->funnelActionElementAssembler->assembleStack(
$funnelWorkerData->getChannel(),
$funnelWorkerData->getChannelProcessor(),
$context
);
}
protected function buildSubmissionEvent(
Request $request,
OutputWorkflowInterface $outputWorkflow,
FormStorageData $formStorageData
): SubmissionEvent {
$formRuntimeData = $formStorageData->getFormRuntimeData() ?? [];
$formData = $this->formValuesInputApplier->apply($formStorageData->getFormData(), $outputWorkflow->getFormDefinition());
$form = $this->frontendFormBuilder->buildForm(
$outputWorkflow->getFormDefinition(),
$formRuntimeData,
[],
$formData
);
return new SubmissionEvent($request, $formRuntimeData, $form, $formStorageData->getFunnelRuntimeData());
}
protected function getStorageProvider(): StorageProviderInterface
{
$funnelConfiguration = $this->configuration->getConfig('funnel');
return $this->storageProviderRegistry->get($funnelConfiguration['storage_provider']);
}
}