Passed
Push — develop ( c023ce...9576d7 )
by Daniel
06:40
created

FormSubmitPost::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Controller;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form;
7
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\FormView;
8
use Silverback\ApiComponentBundle\Factory\Form\FormFactory;
9
use Silverback\ApiComponentBundle\Form\Handler\FormHandlerInterface;
10
use Silverback\ApiComponentBundle\Validator\ClassNameValidator;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Serializer\SerializerInterface;
14
15
class FormSubmitPost extends AbstractForm
16
{
17
    /**
18
     * @var iterable|FormHandlerInterface[]
19
     */
20
    private $handlers;
21
22 2
    public function __construct(
23
        EntityManagerInterface $entityManager,
24
        SerializerInterface $serializer,
25
        FormFactory $formFactory,
26
        iterable $formHandlers
27
    ) {
28 2
        parent::__construct($entityManager, $serializer, $formFactory);
29 2
        $this->handlers = $formHandlers;
30 2
    }
31
32
    /**
33
     * @param Request $request
34
     * @param Form $data
35
     * @return Response
36
     * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
37
     * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException
38
     * @throws \UnexpectedValueException
39
     * @throws \InvalidArgumentException
40
     * @throws \Symfony\Component\Form\Exception\LogicException
41
     * @throws \BadMethodCallException
42
     * @throws \ReflectionException
43
     * @throws \LogicException
44
     */
45 2
    public function __invoke(Request $request, Form $data)
46
    {
47 2
        $contentType = $request->headers->get('CONTENT_TYPE');
48 2
        $_format = $request->attributes->get('_format') ?: $request->getFormat($contentType);
49
50 2
        $form = $this->formFactory->create($data);
51 2
        $formData = $this->deserializeFormData($form, $request->getContent());
52 2
        $form->submit($formData);
53 2
        if (!$form->isSubmitted()) {
54
            return $this->getResponse($data, $_format, false);
55
        }
56 2
        $valid = $form->isValid();
57 2
        $data->setForm(new FormView($form->createView()));
58 2
        if ($valid && $data->getSuccessHandler()) {
59 1
            foreach ($this->handlers as $handler) {
60 1
                if (ClassNameValidator::isClassSame($data->getSuccessHandler(), $handler)) {
61 1
                    $handler->success($data);
62 1
                    break;
63
                }
64
            }
65
        }
66 2
        return $this->getResponse($data, $_format, $valid);
67
    }
68
}
69