Passed
Push — develop ( 75d1fe...ccc913 )
by Daniel
06:14
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form;
8
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\FormView;
9
use Silverback\ApiComponentBundle\Factory\Form\FormFactory;
10
use Silverback\ApiComponentBundle\Form\Handler\FormHandlerInterface;
11
use Silverback\ApiComponentBundle\Validator\ClassNameValidator;
12
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Symfony\Component\Serializer\SerializerInterface;
17
18
class FormSubmitPost extends AbstractForm implements ServiceSubscriberInterface
19
{
20
    /**
21
     * @var iterable|FormHandlerInterface[]
22
     */
23
    private $handlers;
24
25 1
    public function __construct(
26
        EntityManagerInterface $entityManager,
27
        SerializerInterface $serializer,
28
        FormFactory $formFactory,
29
        iterable $formHandlers
30
    ) {
31 1
        parent::__construct($entityManager, $serializer, $formFactory);
32 1
        $this->handlers = $formHandlers;
33 1
    }
34
35
    /**
36
     * @Route(
37
     *     name="silverback_api_component_form_submit",
38
     *     path="/component/forms/{id}/submit.{_format}",
39
     *     requirements={"id"="[^/]+"},
40
     *     defaults={
41
     *         "_api_resource_class"=Form::class,
42
     *         "_api_item_operation_name"="validate_form",
43
     *         "_format"="jsonld"
44
     *     }
45
     * )
46
     * @Method("POST")
47
     * @param Request $request
48
     * @param Form $data
49
     * @param string $_format
50
     * @return Response
51
     * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
52
     * @throws \Symfony\Component\Form\Exception\AlreadySubmittedException
53
     * @throws \UnexpectedValueException
54
     * @throws \InvalidArgumentException
55
     * @throws \Symfony\Component\Form\Exception\LogicException
56
     * @throws \BadMethodCallException
57
     * @throws \ReflectionException
58
     * @throws \LogicException
59
     */
60 1
    public function __invoke(Request $request, Form $data, string $_format)
61
    {
62 1
        $form = $this->formFactory->create($data);
63 1
        $formData = $this->deserializeFormData($form, $request->getContent());
64 1
        $form->submit($formData);
65 1
        if (!$form->isSubmitted()) {
66
            return $this->getResponse($data, $_format, false);
67
        }
68 1
        $valid = $form->isValid();
69 1
        $data->setForm(new FormView($form->createView()));
70 1
        if ($valid && $data->getSuccessHandler()) {
71 1
            foreach ($this->handlers as $handler) {
72 1
                if (ClassNameValidator::isClassSame($data->getSuccessHandler(), $handler)) {
73 1
                    $handler->success($data);
74 1
                    break;
75
                }
76
            }
77
        }
78 1
        return $this->getResponse($data, $_format, $valid);
79
    }
80
}
81