Completed
Push — master ( 0be8e1...585ca2 )
by Daniel
59:26 queued 46:02
created

FormPostAction::__invoke()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 11
nop 2
dl 0
loc 33
ccs 0
cts 17
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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