Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

AbstractMutationResolver::initialFormData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
crap 3
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\EventDispatcher\EventSubscriberInterface;
14
use Symfony\Component\Form\FormBuilderInterface;
15
use Symfony\Component\Form\FormEvent;
16
use Symfony\Component\Form\FormEvents;
17
use Symfony\Component\Form\FormInterface;
18
use Symfony\Component\Validator\ConstraintViolation as SymfonyConstraintViolation;
19
use Ynlo\GraphQLBundle\Error\ErrorQueue;
20
use Ynlo\GraphQLBundle\Events\GraphQLEvents;
21
use Ynlo\GraphQLBundle\Events\GraphQLMutationEvent;
22
use Ynlo\GraphQLBundle\Exception\Controlled\ValidationError;
23
use Ynlo\GraphQLBundle\Model\ConstraintViolation;
24
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
25
use Ynlo\GraphQLBundle\Util\IDEncoder;
26
use Ynlo\GraphQLBundle\Util\Uuid;
27
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList;
28
29
/**
30
 * Base class for mutations
31
 * Implement the method "process()" and "returnPayload()" is enough in many scenarios
32
 */
33
abstract class AbstractMutationResolver extends AbstractResolver implements EventSubscriberInterface
34
{
35
    /**
36
     * @param array $input
37
     *
38
     * @return mixed
39
     */
40 8
    public function __invoke($input)
41
    {
42 8
        $formBuilder = $this->createDefinitionForm($this->initialFormData($input));
43 8
        $mutationEvent = null;
44
45 8
        $form = null;
46 8
        if ($formBuilder) {
47 8
            $formBuilder->addEventListener(
48 8
                FormEvents::SUBMIT,
49 8
                function (FormEvent $event) use (&$mutationEvent) {
50 8
                    if ($this->eventDispatcher) {
51 8
                        $mutationEvent = new GraphQLMutationEvent($this->context, $event);
52 8
                        $this->eventDispatcher->dispatch(GraphQLEvents::MUTATION_SUBMITTED, $mutationEvent);
53
                    }
54 8
                }
55
            );
56
57 8
            $formBuilder->addEventSubscriber($this);
58
59
            $extensionExecutor = function ($method) {
60 8
                return function (FormEvent $event) use ($method) {
61 8
                    foreach ($this->extensions as $extension) {
62 4
                        return call_user_func_array([$extension, $method], [$event]);
63
                    }
64 8
                };
65 8
            };
66
67 8
            foreach (self::getSubscribedEvents() as $event => $method) {
68 8
                $formBuilder->addEventListener($event, $extensionExecutor($method));
69
            }
70
71 8
            $form = $formBuilder->getForm();
72
        }
73
74 8
        if ($form) {
75 8
            $form->submit($input, false);
76 8
            $data = $form->getData();
77
        } else {
78
            $data = $input;
79
        }
80
81 8
        $violations = new ConstraintViolationList();
82 8
        if ($form) {
83 8
            $this->extractFormErrors($form, $violations);
84
        }
85
86 8
        $dryRun = $input['dryRun'] ?? false;
87
88 8
        if ($violations->count()) {
89 1
            $errorHandling = $this->container->getParameter('graphql.error_handling');
90 1
            if (\in_array($errorHandling['validation_messages'] ?? null, ['both', 'error'])) {
91 1
                ErrorQueue::throw(new ValidationError($violations));
92
            }
93
        }
94
95 8
        if ($dryRun) {
96
            $data = null;
97
        } else {
98 8
            if ((!$form && !$violations->count())
99 8
                || ($form->isSubmitted() && $form->isValid() && !$violations->count())
100
            ) {
101 7
                $this->process($data);
102
            }
103
        }
104
105 8
        $payload = $this->returnPayload($data, $violations, $input);
106
107 8
        if ($mutationEvent instanceof GraphQLMutationEvent) {
108 8
            $mutationEvent->setPayload($payload);
109 8
            $this->eventDispatcher->dispatch(GraphQLEvents::MUTATION_COMPLETED, $mutationEvent);
110 8
            $payload = $mutationEvent->getPayload();
111
        }
112
113 8
        return $payload;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 8
    public static function getSubscribedEvents()
120
    {
121
        return [
122 8
            FormEvents::PRE_SET_DATA => 'preSetData',
123 8
            FormEvents::POST_SET_DATA => 'postSetData',
124 8
            FormEvents::PRE_SUBMIT => 'preSubmit',
125 8
            FormEvents::SUBMIT => 'onSubmit',
126 8
            FormEvents::POST_SUBMIT => 'postSubmit',
127
        ];
128
    }
129
130
    /**
131
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
132
     *
133
     * @param FormEvent $event
134
     */
135 8
    public function preSetData(FormEvent $event)
136
    {
137 8
    }
138
139
    /**
140
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
141
     *
142
     * @param FormEvent $event
143
     */
144 8
    public function postSetData(FormEvent $event)
145
    {
146 8
    }
147
148
    /**
149
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
150
     *
151
     * @param FormEvent $event
152
     */
153 8
    public function preSubmit(FormEvent $event)
154
    {
155 8
    }
156
157
    /**
158
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
159
     *
160
     * @param FormEvent $event
161
     */
162 4
    public function onSubmit(FormEvent $event)
163
    {
164 4
    }
165
166
    /**
167
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
168
     *
169
     * @param FormEvent $event
170
     */
171 8
    public function postSubmit(FormEvent $event)
172
    {
173 8
    }
174
175
    /**
176
     * Actions to process
177
     * the result processed data is given to payload
178
     *
179
     * @param mixed $data
180
     */
181
    abstract public function process(&$data);
182
183
    /**
184
     * The payload object or array matching the GraphQL definition
185
     *
186
     * @param mixed                   $data        normalized data, its the input data processed by the form
187
     * @param ConstraintViolationList $violations  violations returned by the form validation process
188
     * @param array                   $inputSource the original submitted data in array
189
     *
190
     * @return mixed
191
     */
192
    abstract public function returnPayload($data, ConstraintViolationList $violations, $inputSource);
193
194
    /**
195
     * @return null|string
196
     */
197 8
    protected function getPayloadClass(): string
198
    {
199 8
        $type = $this->getContext()->getDefinition()->getType();
200
201 8
        if (class_exists($type)) {
202 3
            return $type;
203
        }
204
205 5
        if ($this->context->getEndpoint()->hasType($type)) {
206 5
            $class = $this->context->getEndpoint()->getClassForType($type);
207 5
            if (class_exists($class)) {
208 5
                return $class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $class could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
209
            }
210
        }
211
212
        throw new \RuntimeException(
213
            sprintf(
214
                'Can\'t find a valid payload class for "%s".',
215
                $this->getContext()->getDefinition()->getName()
216
            )
217
        );
218
    }
219
220
    /**
221
     * @param array $input
222
     *
223
     * @return mixed
224
     */
225 8
    public function initialFormData($input)
226
    {
227 8
        if (is_array($input) && isset($input['id'])) {
228 3
            return IDEncoder::decode($input['id']);
229
        }
230
231 6
        return null;
232
    }
233
    /**
234
     * @param mixed|null $data
235
     *
236
     * @return FormBuilderInterface|null
237
     */
238 8
    public function createDefinitionForm($data): ?FormBuilderInterface
239
    {
240 8
        if (!$this->context->getDefinition()->hasMeta('form')) {
241
            return null;
242
        }
243
244 8
        $formConfig = $this->context->getDefinition()->getMeta('form') ?? [];
245 8
        $formType = $formConfig['type'] ?? null;
246 8
        if (!$formConfig || !$formType) {
247
            throw new \RuntimeException(sprintf('Can`t find a valid form for %s', $this->context->getDefinition()->getName()));
248
        }
249
250
        $options = [
251 8
            'allow_extra_fields' => true,
252
        ];
253
254 8
        if ($this->container->hasParameter('form.type_extension.csrf.enabled')
255 8
            && $this->container->getParameter('form.type_extension.csrf.enabled')) {
256 8
            $options['csrf_protection'] = false;
257
        }
258
259 8
        $options = array_merge($options, $formConfig['options'] ?? []);
260
261 8
        return $this->createFormBuilder($formType, $data, $options);
262
    }
263
264
    /**
265
     * @param FormInterface           $form
266
     * @param ConstraintViolationList $violations
267
     * @param null|string             $parentName
268
     */
269 8
    public function extractFormErrors(FormInterface $form, ConstraintViolationList $violations, ?string $parentName = null)
270
    {
271 8
        $errors = $form->getErrors(true);
272 8
        foreach ($errors as $error) {
273 1
            $violation = new ConstraintViolation();
274 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

274
            $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...
275 1
            $violation->setMessageTemplate($error->getMessageTemplate() ?? $error->getMessage());
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

275
            $violation->setMessageTemplate($error->/** @scrutinizer ignore-call */ getMessageTemplate() ?? $error->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...
276 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

276
            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...
277 1
                $violation->addParameter($key, $value);
278
            }
279
280 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

280
            /** @scrutinizer ignore-call */ 
281
            $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...
281 1
            if ($cause instanceof SymfonyConstraintViolation) {
282 1
                $violation->setCode($cause->getCode() ?? Uuid::createFromData($violation->getMessageTemplate()));
283 1
                $violation->setInvalidValue($cause->getInvalidValue());
284 1
                $violation->setPlural($cause->getPlural());
285
286 1
                $path = $this->publicPropertyPath($form, $cause->getPropertyPath());
287 1
                if ($path) {
288 1
                    $violation->setPropertyPath($path);
289
                }
290
            }
291
292 1
            $violations->addViolation($violation);
293
        }
294 8
    }
295
296
    /**
297
     * Convert internal validation property path to the public one,
298
     * required when use `property_path` in the form
299
     *
300
     * @param FormInterface $form
301
     * @param string        $path
302
     *
303
     * @return string
304
     */
305 1
    private function publicPropertyPath(FormInterface $form, $path)
306
    {
307 1
        $pathArray = [$path];
308
309 1
        if (strpos($path, '.') !== false) { // object.child.property
310 1
            $pathArray = explode('.', $path);
311
        }
312
313 1
        if (strpos($path, '[') !== false) { //[array][child][property]
314 1
            $path = str_replace(']', null, $path);
315 1
            $pathArray = explode('[', $path);
316
        }
317
318 1
        if (in_array($pathArray[0], ['data', 'children'])) {
319 1
            array_shift($pathArray);
320
        }
321
322 1
        $contextForm = $form;
323 1
        foreach ($pathArray as &$propName) {
324
            //for some reason some inputs are resolved as "inputName.data"
325
            //because the original form property is children[inputName].data
326
            //this is the case of DEMO AddUserInput form the login field is validated as path children[login].data
327
            //the following statements remove the trailing ".data"
328 1
            if (preg_match('/\.data$/', $propName)) {
329 1
                $propName = preg_replace('/\.data$/', null, $propName);
330
            }
331
332 1
            $index = null;
333 1
            if (preg_match('/(\w+)(\[\d+\])$/', $propName, $matches)) {
334
                list(, $propName, $index) = $matches;
335
            }
336 1
            if (!$contextForm->has($propName)) {
337
                foreach ($contextForm->all() as $child) {
338
                    if ($child->getConfig()->getOption('property_path') === $propName) {
339
                        $propName = $child->getName();
340
                    }
341
                }
342
            }
343 1
            if ($index) {
344 1
                $propName = sprintf('%s%s', $propName, $index);
0 ignored issues
show
Bug introduced by
$index of type void is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

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

344
                $propName = sprintf('%s%s', $propName, /** @scrutinizer ignore-type */ $index);
Loading history...
345
            }
346
        }
347 1
        unset($propName);
348
349 1
        return implode('.', $pathArray);
350
    }
351
}
352