Completed
Push — feature/other-validation ( b701a5...b5bedb )
by Narcotic
63:49
created

Form::checkJsonPatchRequest()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 8.8571
cc 5
eloc 6
nc 4
nop 1
crap 30
1
<?php
2
/**
3
 * base form validator
4
 */
5
6
namespace Graviton\RestBundle\Validator;
7
8
use Graviton\DocumentBundle\Service\FormDataMapperInterface;
9
use Graviton\ExceptionBundle\Exception\InvalidJsonPatchException;
10
use Graviton\ExceptionBundle\Exception\MalformedInputException;
11
use Graviton\ExceptionBundle\Exception\NoInputException;
12
use Graviton\ExceptionBundle\Exception\ValidationException;
13
use Graviton\RestBundle\Model\DocumentModel;
14
use Symfony\Component\Form\FormFactory;
15
use Symfony\Component\Form\FormInterface;
16
use Graviton\DocumentBundle\Form\Type\DocumentType;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Validator\Validator\ValidatorInterface;
20
21
/**
22
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
23
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
24
 * @link     http://swisscom.ch
25
 */
26
class Form
27
{
28
    /**
29
     * @var FormFactory
30
     */
31
    private $formFactory;
32
33
    /**
34
     * @var DocumentType
35
     */
36
    private $formType;
37
38
    /**
39
     * @var ValidatorInterface
40
     */
41
    private $validator;
42
43
    /**
44
     * @param FormFactory        $formFactory Factory, providing different file document instances.
45
     * @param DocumentType       $formType    Type of form to be set
46
     * @param ValidatorInterface $validator   Validator to verify correctness of the provided data
47
     */
48
    public function __construct(
49
        FormFactory $formFactory,
50
        DocumentType $formType,
51
        ValidatorInterface $validator
52
    ) {
53
        $this->formFactory = $formFactory;
54
        $this->formType = $formType;
55
        $this->validator = $validator;
56
    }
57
58
    /**
59
     * @param Request       $request request
60
     * @param DocumentModel $model   model
61
     *
62
     * @return \Symfony\Component\Form\Form
63
     */
64
    public function getForm(Request $request, DocumentModel $model)
65
    {
66
        $this->formType->initialize($model->getEntityClass());
67
        return $this->formFactory->create($this->formType, null, ['method' => $request->getMethod()]);
0 ignored issues
show
Documentation introduced by
$this->formType is of type object<Graviton\Document...Form\Type\DocumentType>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
    }
69
70
    /**
71
     * Validates the provided information against a form.
72
     *
73
     * @param FormInterface           $form           form to check
74
     * @param DocumentModel           $model          Model to determine entity to be used
75
     * @param FormDataMapperInterface $formDataMapper Mapps the entity to form fields
76
     * @param string                  $jsonContent    json data
77
     *
78
     * @throws ValidationException
79
     * @return mixed
80
     */
81
    public function checkForm(
82
        FormInterface $form,
83
        DocumentModel $model,
84
        FormDataMapperInterface $formDataMapper,
85
        $jsonContent
86
    ) {
87
        $document = $formDataMapper->convertToFormData(
88
            $jsonContent,
89
            $model->getEntityClass()
90
        );
91
        $form->submit($document, true);
92
93
        if (!$form->isValid()) {
94
            throw new ValidationException($form->getErrors(true));
95
        } else {
96
            $record = $form->getData();
97
        }
98
99
        return $record;
100
    }
101
}
102