Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

FormSubmitPost::__invoke()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 5
nop 3
dl 0
loc 19
rs 8.8571
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\Component\Form\Form;
8
use Silverback\ApiComponentBundle\Entity\Component\Form\FormView;
9
use Silverback\ApiComponentBundle\Factory\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 FormHandlerInterface[]
22
     */
23
    private $handlers;
24
25
    public function __construct(
26
        EntityManagerInterface $entityManager,
27
        SerializerInterface $serializer,
28
        FormFactory $formFactory,
29
        iterable $formHandlers
30
    ) {
31
        parent::__construct($entityManager, $serializer, $formFactory);
32
        $this->handlers = $formHandlers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $formHandlers of type iterable is incompatible with the declared type Silverback\ApiComponentB...\FormHandlerInterface[] of property $handlers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
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
    public function __invoke(Request $request, Form $data, string $_format)
61
    {
62
        $form = $this->formFactory->createForm($data);
63
        $formData = $this->deserializeFormData($form, $request->getContent());
64
        $form->submit($formData);
65
        if (!$form->isSubmitted()) {
66
            return $this->getResponse($data, $_format, false);
67
        }
68
        $valid = $form->isValid();
69
        $data->setForm(new FormView($form->createView()));
70
        if ($valid && $data->getSuccessHandler()) {
71
            foreach ($this->handlers as $handler) {
72
                if (ClassNameValidator::isClassSame($data->getSuccessHandler(), $handler)) {
73
                    $handler->success($data);
74
                    break;
75
                }
76
            }
77
        }
78
        return $this->getResponse($data, $_format, $valid);
79
    }
80
}
81