-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathSubmitHandler.php
154 lines (130 loc) · 5.52 KB
/
SubmitHandler.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
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace EzSystems\EzPlatformAdminUi\Form;
use Exception;
use eZ\Publish\API\Repository\Exceptions\ForbiddenException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface;
use EzSystems\EzPlatformAdminUi\UI\Action\EventDispatcherInterface;
use EzSystems\EzPlatformAdminUi\UI\Action\FormUiActionMappingDispatcher;
use EzSystems\EzPlatformAdminUi\UI\Action\UiActionEventInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
class SubmitHandler
{
/** @var \EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface */
protected $notificationHandler;
/** @var \Symfony\Component\Routing\RouterInterface */
protected $router;
/** @var \EzSystems\EzPlatformAdminUi\UI\Action\EventDispatcherInterface */
protected $uiActionEventDispatcher;
/** @var \EzSystems\EzPlatformAdminUi\UI\Action\FormUiActionMappingDispatcher */
protected $formUiActionMappingDispatcher;
/** @var \Psr\Log\LoggerInterface */
private $logger;
public function __construct(
NotificationHandlerInterface $notificationHandler,
RouterInterface $router,
EventDispatcherInterface $uiActionEventDispatcher,
FormUiActionMappingDispatcher $formUiActionMappingDispatcher,
LoggerInterface $logger
) {
$this->notificationHandler = $notificationHandler;
$this->router = $router;
$this->uiActionEventDispatcher = $uiActionEventDispatcher;
$this->formUiActionMappingDispatcher = $formUiActionMappingDispatcher;
$this->logger = $logger;
}
/**
* Wraps business logic with reusable boilerplate code.
*
* Handles form errors (NotificationHandler:warning).
* Handles business logic exceptions (NotificationHandler:error).
*
* @param \Symfony\Component\Form\FormInterface $form
* @param callable(mixed):?Response $handler
*
* @return \Symfony\Component\HttpFoundation\Response|null
*/
public function handle(FormInterface $form, callable $handler): ?Response
{
$data = $form->getData();
if ($form->isValid()) {
try {
$result = $handler($data);
if ($result instanceof Response) {
$event = $this->formUiActionMappingDispatcher->dispatch($form);
$event->setResponse($result);
$event->setType(UiActionEventInterface::TYPE_SUCCESS);
$this->uiActionEventDispatcher->dispatch($event);
return $event->getResponse();
}
} catch (ForbiddenException | NotFoundException | UnauthorizedException $e) {
$this->notificationHandler->error(/** @Ignore */ $e->getMessage());
} catch (Exception $e) {
$this->logException($e);
$this->notificationHandler->error(/** @Ignore */ $e->getMessage());
}
} else {
foreach ($form->getErrors(true, true) as $formError) {
$this->notificationHandler->warning(/** @Ignore */ $formError->getMessage());
}
}
return null;
}
/**
* Wraps business logic with reusable boilerplate code.
*
* Handles form errors (JsonResponse(['errors'=> [...], Response::ERROR_STATUS_CODE])).
* Handles business logic exceptions (JsonResponse(['errors'=> [...], Response::ERROR_STATUS_CODE])).
*
* @param \Symfony\Component\Form\FormInterface $form
* @param callable(mixed):?Response $handler
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function handleAjax(FormInterface $form, callable $handler): JsonResponse
{
$data = $form->getData();
if ($form->isValid()) {
try {
/** @var \Symfony\Component\HttpFoundation\JsonResponse $result */
$result = $handler($data);
if ($result instanceof JsonResponse && $result->getStatusCode() === Response::HTTP_OK) {
$event = $this->formUiActionMappingDispatcher->dispatch($form);
$event->setResponse($result);
$event->setType(UiActionEventInterface::TYPE_SUCCESS);
$this->uiActionEventDispatcher->dispatch($event);
return $event->getResponse();
}
return $result;
} catch (Exception $e) {
$this->logException($e);
return new JsonResponse([], Response::HTTP_BAD_REQUEST);
}
} else {
$errors = [];
foreach ($form->getErrors(true, true) as $formError) {
$errors[] = $formError->getMessage();
}
return new JsonResponse(['errors' => $errors], Response::HTTP_UNPROCESSABLE_ENTITY);
}
}
protected function logException(Exception $e): void
{
$this->logger->error(
'An error has occurred while handling form submission: ' . $e->getMessage(),
[
'exception' => $e->getPrevious() ?? $e,
]
);
}
}