1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Silverback\ApiComponentBundle\Dto\Form\FormView; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\Form\Form; |
8
|
|
|
use Silverback\ApiComponentBundle\Factory\Form\FormFactory; |
9
|
|
|
use Silverback\ApiComponentBundle\Form\Handler\ContextProviderInterface; |
10
|
|
|
use Silverback\ApiComponentBundle\Form\Handler\FormHandlerInterface; |
11
|
|
|
use Silverback\ApiComponentBundle\Validator\ClassNameValidator; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
15
|
|
|
|
16
|
|
|
class FormPostAction extends AbstractFormAction |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var iterable|FormHandlerInterface[] |
20
|
|
|
*/ |
21
|
|
|
private $handlers; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
EntityManagerInterface $entityManager, |
25
|
|
|
SerializerInterface $serializer, |
26
|
|
|
FormFactory $formFactory, |
27
|
|
|
iterable $formHandlers |
28
|
|
|
) { |
29
|
|
|
parent::__construct($entityManager, $serializer, $formFactory); |
30
|
|
|
$this->handlers = $formHandlers; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Request $request |
35
|
|
|
* @param Form $data |
36
|
|
|
* @return Response |
37
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
38
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
39
|
|
|
* @throws \UnexpectedValueException |
40
|
|
|
* @throws \InvalidArgumentException |
41
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
42
|
|
|
* @throws \BadMethodCallException |
43
|
|
|
* @throws \LogicException |
44
|
|
|
*/ |
45
|
|
|
public function __invoke(Request $request, Form $data) |
46
|
|
|
{ |
47
|
|
|
$contentType = $request->headers->get('CONTENT_TYPE'); |
48
|
|
|
$_format = $request->attributes->get('_format') ?: $request->getFormat($contentType); |
49
|
|
|
|
50
|
|
|
$builder = $this->formFactory->create($data); |
51
|
|
|
$form = $builder->getForm(); |
52
|
|
|
$formData = $this->deserializeFormData($form, $request->getContent()); |
53
|
|
|
$form->submit($formData); |
54
|
|
|
if (!$form->isSubmitted()) { |
55
|
|
|
return $this->getResponse($data, $_format, false); |
56
|
|
|
} |
57
|
|
|
$valid = $form->isValid(); |
58
|
|
|
$data->setForm(new FormView($form->createView())); |
59
|
|
|
$context = null; |
60
|
|
|
if ($valid && $data->getSuccessHandler()) { |
61
|
|
|
foreach ($this->handlers as $handler) { |
62
|
|
|
if (ClassNameValidator::isClassSame($data->getSuccessHandler(), $handler)) { |
63
|
|
|
$result = $handler->success($data, $form->getData(), $request); |
64
|
|
|
if ($handler instanceof ContextProviderInterface) { |
65
|
|
|
$context = $handler->getContext(); |
66
|
|
|
} |
67
|
|
|
if ($result instanceof Response) { |
68
|
|
|
return $result; |
69
|
|
|
} |
70
|
|
|
if ($result) { |
71
|
|
|
$data = $result; |
72
|
|
|
} |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $this->getResponse($data, $_format, $valid, null, null, $context); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|