Test Failed
Push — develop ( f6d190...8ff4ed )
by Daniel
04:04
created

AbstractFormAction::getResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 6
dl 0
loc 14
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 = ['groups' => ['component']]
63
    ): Response {
64
        if (!$response) {
65
            $response = new Response();
66
        }
67
        $response->setStatusCode($statusCode ?? ($valid ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST));
68
        $response->setContent($this->serializer->serialize($data, $_format, $context));
69
        return $response;
70
    }
71
72
    /**
73
     * @param \Silverback\ApiComponentBundle\DTO\FormView $formView
0 ignored issues
show
Bug introduced by
The type Silverback\ApiComponentBundle\DTO\FormView was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
     * @return mixed
75
     */
76
    protected function getFormValid(FormView $formView)
77
    {
78
        return $formView->getVars()['valid'];
79
    }
80
81
    /**
82
     * @param FormInterface $form
83
     * @param $content
84
     * @return array
85
     * @throws BadRequestHttpException
86
     */
87
    public function deserializeFormData(FormInterface $form, $content): array
88
    {
89
        $content = \GuzzleHttp\json_decode($content, true);
90
        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
        return $content[$form->getName()];
96
    }
97
}
98