Passed
Push — develop ( 185343...eb895f )
by Daniel
06:30
created

AbstractForm::getResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 6
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 3
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\FormView;
7
use Silverback\ApiComponentBundle\Factory\Form\FormFactory;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\Form\FormInterface;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
use Symfony\Component\Serializer\SerializerInterface;
13
14
abstract class AbstractForm extends AbstractController
15
{
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    protected $entityManager;
20
21
    /**
22
     * @var SerializerInterface
23
     */
24
    protected $serializer;
25
26
    /**
27
     * @var FormFactory
28
     */
29
    protected $formFactory;
30
31
    /**
32
     * AbstractForm constructor.
33
     * @param EntityManagerInterface $entityManager
34
     * @param SerializerInterface $serializer
35
     * @param FormFactory $formFactory
36
     */
37 4
    public function __construct(
38
        EntityManagerInterface $entityManager,
39
        SerializerInterface $serializer,
40
        FormFactory $formFactory
41
    ) {
42 4
        $this->entityManager = $entityManager;
43 4
        $this->serializer = $serializer;
44 4
        $this->formFactory = $formFactory;
45 4
    }
46
47
    /**
48
     * @param $data
49
     * @param $_format
50
     * @param $valid
51
     * @param Response|null $response
52
     * @param int|null $statusCode
53
     * @param array|null $context
54
     * @return Response
55
     */
56 4
    protected function getResponse(
57
        $data,
58
        $_format,
59
        $valid,
60
        Response $response = null,
61
        ?int $statusCode = null,
62
        ?array $context = ['groups' => ['component']]
63
    ): Response {
64 4
        if (!$response) {
65 4
            $response = new Response();
66
        }
67 4
        $response->setStatusCode($statusCode ?? ($valid ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST));
68 4
        $response->setContent($this->serializer->serialize($data, $_format, $context));
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type null; however, parameter $context of Symfony\Component\Serial...rInterface::serialize() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $response->setContent($this->serializer->serialize($data, $_format, /** @scrutinizer ignore-type */ $context));
Loading history...
69 4
        return $response;
70
    }
71
72
    /**
73
     * @param FormView $formView
74
     * @return mixed
75
     */
76 2
    protected function getFormValid(FormView $formView)
77
    {
78 2
        return $formView->getVars()['valid'];
79
    }
80
81
    /**
82
     * @param FormInterface $form
83
     * @param $content
84
     * @return array
85
     * @throws BadRequestHttpException
86
     */
87 4
    public function deserializeFormData(FormInterface $form, $content): array
88
    {
89 4
        $content = \GuzzleHttp\json_decode($content, true);
90 4
        if (!isset($content[$form->getName()])) {
91
            throw new BadRequestHttpException(
92
                sprintf('Form object key could not be found. Expected: <b>%s</b>: { "input_name": "input_value" }', $form->getName())
93
            );
94
        }
95 4
        return $content[$form->getName()];
96
    }
97
}
98