Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

AbstractMutationAbstractResolver::postValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Mutation;
12
13
use Symfony\Component\Form\FormBuilderInterface;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\Validator\ConstraintViolation as SymfonyConstraintViolation;
16
use Ynlo\GraphQLBundle\Form\DataTransformer\DataWithIdToNodeTransformer;
17
use Ynlo\GraphQLBundle\Model\ConstraintViolation;
18
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
19
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList;
20
21
/**
22
 * Base class for mutations
23
 * Implement the method "process()" and "returnPayload()" is enough in many scenarios
24
 */
25
abstract class AbstractMutationAbstractResolver extends AbstractResolver
26
{
27
    /**
28
     * @param array $input
29
     *
30
     * @return mixed
31
     */
32 7
    public function __invoke($input)
33
    {
34 7
        $form = $this->createDefinitionForm($input)->getForm();
35
36 7
        $this->preValidate($input);
37 7
        $form->submit($input, false);
38
39 7
        $data = $form->getData();
40 7
        $this->onSubmit($input, $data);
41
42 7
        $violations = new ConstraintViolationList();
43 7
        $this->extractFormErrors($form, $violations);
44 7
        $this->postValidation($data, $violations);
45
46 7
        $dryRun = $input['dryRun'] ?? false;
47
48 7
        if ($dryRun) {
49 2
            $data = null;
50
        } else {
51 5
            if ($form->isSubmitted() && $form->isValid() && !$violations->count()) {
52 5
                $this->process($data);
53
            }
54
        }
55
56 7
        return $this->returnPayload($data, $violations, $input);
57
    }
58
59
    /**
60
     * Actions to process
61
     * the result processed data is given to payload
62
     *
63
     * @param mixed $data
64
     */
65
    abstract protected function process(&$data);
66
67
    /**
68
     * The payload object or array matching the GraphQL definition
69
     *
70
     * @param mixed                   $data        normalized data, its the input data processed by the form
71
     * @param ConstraintViolationList $violations  violations returned by the form validation process
72
     * @param array                   $inputSource the original submitted data in array
73
     *
74
     * @return mixed
75
     */
76
    abstract protected function returnPayload($data, ConstraintViolationList $violations, $inputSource);
77
78
    /**
79
     * @param mixed $data
80
     *
81
     * @return FormBuilderInterface
82
     */
83 7
    protected function createDefinitionForm($data): FormBuilderInterface
84
    {
85 7
        if (!$this->context->getDefinition()->getMeta('form')) {
86
            throw new \RuntimeException(sprintf('Can`t find a valid form for %s', $this->context->getDefinition()->getName()));
87
        }
88
89 7
        $form = $this->context->getDefinition()->getMeta('form');
90
91
        $options = [
92 7
            'csrf_protection' => false,
93
            'allow_extra_fields' => true,
94
        ];
95 7
        if ($this->context->getDefinition()->hasMeta('form_options')) {
96 7
            $options = array_merge($options, $this->context->getDefinition()->getMeta('form_options'));
97
        }
98
99 7
        $form = $this->createFormBuilder($form, $data, $options);
100 7
        $viewTransformer = new DataWithIdToNodeTransformer($this->getManager(), $this->context->getDefinitionManager());
101 7
        $form->addViewTransformer($viewTransformer);
102
103 7
        return $form;
104
    }
105
106
    /**
107
     * @param FormInterface           $form
108
     * @param ConstraintViolationList $violations
109
     * @param null|string             $parentName
110
     */
111 7
    protected function extractFormErrors(FormInterface $form, ConstraintViolationList $violations, ?string $parentName = null)
112
    {
113 7
        $errors = $form->getErrors();
114 7
        foreach ($errors as $error) {
115 1
            $violation = new ConstraintViolation();
116
117 1
            $path = null;
118 1
            if ($form->getParent()) {
119 1
                $path = $form->getName();
120
            }
121 1
            if ($parentName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parentName of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
122 1
                $path = $parentName.'.'.$form->getName();
123
            }
124
125 1
            $violation->setPropertyPath($path);
126 1
            $violation->setMessage($error->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

126
            $violation->setMessage($error->/** @scrutinizer ignore-call */ getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127 1
            $violation->setMessageTemplate($error->getMessageTemplate());
0 ignored issues
show
Bug introduced by
The method getMessageTemplate() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

127
            $violation->setMessageTemplate($error->/** @scrutinizer ignore-call */ getMessageTemplate());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128 1
            foreach ($error->getMessageParameters() as $key => $value) {
0 ignored issues
show
Bug introduced by
The method getMessageParameters() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

128
            foreach ($error->/** @scrutinizer ignore-call */ getMessageParameters() as $key => $value) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129 1
                $violation->addParameter($key, $value);
130
            }
131
132 1
            $cause = $error->getCause();
0 ignored issues
show
Bug introduced by
The method getCause() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

132
            /** @scrutinizer ignore-call */ 
133
            $cause = $error->getCause();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133 1
            if ($cause instanceof SymfonyConstraintViolation) {
134 1
                $violation->setCode($cause->getCode());
135 1
                $violation->setInvalidValue($cause->getInvalidValue());
136 1
                $violation->setPlural($cause->getPlural());
137
            }
138
139 1
            $violations->addViolation($violation);
140
        }
141 7
        if ($form->all()) {
142 7
            foreach ($form->all() as $child) {
143 7
                $parentName = $form->getName();
144
145
                //avoid set the name of the form
146 7
                if ($form->getName() === $parentName && !$form->getParent()) {
147 7
                    $parentName = null;
148
                }
149
150 7
                $this->extractFormErrors($child, $violations, $parentName);
151
            }
152
        }
153 7
    }
154
155
    /**
156
     * Can use this method to verify if submitted data is valid
157
     * otherwise can trow a error
158
     *
159
     * @param mixed $inputSource contain the original submitted input data
160
     * @param mixed $normData    contains the processed and normalized data by the form
161
     */
162 3
    protected function onSubmit($inputSource, &$normData)
0 ignored issues
show
Unused Code introduced by
The parameter $normData is not used and could be removed. ( Ignorable by Annotation )

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

162
    protected function onSubmit($inputSource, /** @scrutinizer ignore-unused */ &$normData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $inputSource is not used and could be removed. ( Ignorable by Annotation )

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

162
    protected function onSubmit(/** @scrutinizer ignore-unused */ $inputSource, &$normData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
    {
164
        //override in child
165 3
    }
166
167
    /**
168
     * Can do something before validate the submitted data
169
     *
170
     * @param mixed $data
171
     */
172 7
    protected function preValidate(&$data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

172
    protected function preValidate(/** @scrutinizer ignore-unused */ &$data)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
    {
174
        //override in child
175 7
    }
176
177
    /**
178
     * Can use this to add your custom validations errors
179
     *
180
     * @param mixed                   $data
181
     * @param ConstraintViolationList $violations
182
     */
183 6
    protected function postValidation($data, ConstraintViolationList $violations)
0 ignored issues
show
Unused Code introduced by
The parameter $violations is not used and could be removed. ( Ignorable by Annotation )

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

183
    protected function postValidation($data, /** @scrutinizer ignore-unused */ ConstraintViolationList $violations)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

183
    protected function postValidation(/** @scrutinizer ignore-unused */ $data, ConstraintViolationList $violations)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
        //override in child
186 6
    }
187
}
188