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

AbstractFormAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\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 AbstractFormAction 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
    public function __construct(
38
        EntityManagerInterface $entityManager,
39
        SerializerInterface $serializer,
40
        FormFactory $formFactory
41
    ) {
42
        $this->entityManager = $entityManager;
43
        $this->serializer = $serializer;
44
        $this->formFactory = $formFactory;
45
    }
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
    protected function getResponse(
57
        $data,
58
        $_format,
59
        $valid,
60
        Response $response = null,
61
        ?int $statusCode = null,
62
        ?array $context = null
63
    ): Response {
64
        if (!$response) {
65
            $response = new Response();
66
        }
67
        if (!$context) {
68
            $context = ['groups' => ['component']];
69
        }
70
        $response->setStatusCode($statusCode ?? ($valid ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST));
71
        $response->setContent($this->serializer->serialize($data, $_format, $context));
72
        return $response;
73
    }
74
75
    /**
76
     * @param \Silverback\ApiComponentBundle\Dto\Form\FormView $formView
77
     * @return mixed
78
     */
79
    protected function getFormValid(FormView $formView)
80
    {
81
        return $formView->getVars()['valid'];
82
    }
83
84
    /**
85
     * @param FormInterface $form
86
     * @param $content
87
     * @return array
88
     * @throws BadRequestHttpException
89
     */
90
    public function deserializeFormData(FormInterface $form, $content): array
91
    {
92
        $content = \GuzzleHttp\json_decode($content, true);
93
        if (!isset($content[$form->getName()])) {
94
            throw new BadRequestHttpException(
95
                sprintf('Form object key could not be found. Expected: <b>%s</b>: { "input_name": "input_value" }', $form->getName())
96
            );
97
        }
98
        return $content[$form->getName()];
99
    }
100
}
101