Passed
Push — develop ( 75d1fe...ccc913 )
by Daniel
06:14
created

AbstractForm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deserializeFormData() 0 9 2
A getResponse() 0 8 3
A getFormValid() 0 3 1
A __construct() 0 8 1
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 2
    public function __construct(
38
        EntityManagerInterface $entityManager,
39
        SerializerInterface $serializer,
40
        FormFactory $formFactory
41
    ) {
42 2
        $this->entityManager = $entityManager;
43 2
        $this->serializer = $serializer;
44 2
        $this->formFactory = $formFactory;
45 2
    }
46
47
    /**
48
     * @param $data
49
     * @param $_format
50
     * @param $valid
51
     * @param Response|null $response
52
     * @return Response
53
     * @throws \InvalidArgumentException
54
     * @throws \UnexpectedValueException
55
     */
56 2
    protected function getResponse($data, $_format, $valid, Response $response = null): Response
57
    {
58 2
        if (!$response) {
59 2
            $response = new Response();
60
        }
61 2
        $response->setStatusCode($valid ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST);
62 2
        $response->setContent($this->serializer->serialize($data, $_format, ['groups' => ['page']]));
63 2
        return $response;
64
    }
65
66
    /**
67
     * @param FormView $formView
68
     * @return mixed
69
     */
70 1
    protected function getFormValid(FormView $formView)
71
    {
72 1
        return $formView->getVars()['valid'];
73
    }
74
75
    /**
76
     * @param FormInterface $form
77
     * @param $content
78
     * @return array
79
     * @throws BadRequestHttpException
80
     */
81 2
    public function deserializeFormData(FormInterface $form, $content): array
82
    {
83 2
        $content = \GuzzleHttp\json_decode($content, true);
84 2
        if (!isset($content[$form->getName()])) {
85
            throw new BadRequestHttpException(
86
                sprintf('Form object key could not be found. Expected: <b>%s</b>: { "input_name": "input_value" }', $form->getName())
87
            );
88
        }
89 2
        return $content[$form->getName()];
90
    }
91
}
92