Passed
Push — master ( ea8174...8dbfa1 )
by Rafael
05:24
created

AbstractMutationResolver   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Test Coverage

Coverage 94.32%

Importance

Changes 0
Metric Value
wmc 38
dl 0
loc 242
ccs 83
cts 88
cp 0.9432
rs 8.3999
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 8 1
A onSubmit() 0 2 1
A preSubmit() 0 2 1
A postSubmit() 0 2 1
A postSetData() 0 2 1
A preSetData() 0 2 1
C __invoke() 0 48 12
D publicPropertyPath() 0 31 9
B createDefinitionForm() 0 28 6
B extractFormErrors() 0 24 5
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\Form\DataTransformer\DataWithIdToNodeTransformer;
20
use Ynlo\GraphQLBundle\Model\ConstraintViolation;
21
use Ynlo\GraphQLBundle\Resolver\AbstractResolver;
22
use Ynlo\GraphQLBundle\Validator\ConstraintViolationList;
23
24
/**
25
 * Base class for mutations
26
 * Implement the method "process()" and "returnPayload()" is enough in many scenarios
27
 */
28
abstract class AbstractMutationResolver extends AbstractResolver implements EventSubscriberInterface
29
{
30
    /**
31
     * @param array $input
32
     *
33
     * @return mixed
34
     */
35 8
    public function __invoke($input)
36
    {
37 8
        $formBuilder = $this->createDefinitionForm($input);
38
39 8
        $form = null;
40 8
        if ($formBuilder) {
41 8
            $formBuilder->addEventSubscriber($this);
42
43
            $extensionExecutor = function ($method) {
44 8
                return function (FormEvent $event) use ($method) {
45 8
                    foreach ($this->extensions as $extension) {
46 4
                        return call_user_func_array([$extension, $method], [$event]);
47
                    }
48 8
                };
49 8
            };
50
51 8
            foreach (self::getSubscribedEvents() as $event => $method) {
52 8
                $formBuilder->addEventListener($event, $extensionExecutor($method));
53
            }
54
55 8
            $form = $formBuilder->getForm();
56
        }
57
58 8
        if ($form) {
59 8
            $form->submit($input, false);
60 8
            $data = $form->getData();
61
        } else {
62
            $data = $input;
63
        }
64
65 8
        $violations = new ConstraintViolationList();
66 8
        if ($form) {
67 8
            $this->extractFormErrors($form, $violations);
68
        }
69
70 8
        $dryRun = $input['dryRun'] ?? false;
71
72 8
        if ($dryRun) {
73
            $data = null;
74
        } else {
75 8
            if ((!$form && !$violations->count())
76 8
                || ($form->isSubmitted() && $form->isValid() && !$violations->count())
77
            ) {
78 7
                $this->process($data);
79
            }
80
        }
81
82 8
        return $this->returnPayload($data, $violations, $input);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 8
    public static function getSubscribedEvents()
89
    {
90
        return [
91 8
            FormEvents::PRE_SET_DATA => 'preSetData',
92 8
            FormEvents::POST_SET_DATA => 'postSetData',
93 8
            FormEvents::PRE_SUBMIT => 'preSubmit',
94 8
            FormEvents::SUBMIT => 'onSubmit',
95 8
            FormEvents::POST_SUBMIT => 'postSubmit',
96
        ];
97
    }
98
99
    /**
100
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
101
     *
102
     * @param FormEvent $event
103
     */
104 8
    public function preSetData(FormEvent $event)
105
    {
106 8
    }
107
108
    /**
109
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
110
     *
111
     * @param FormEvent $event
112
     */
113 8
    public function postSetData(FormEvent $event)
114
    {
115 8
    }
116
117
    /**
118
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
119
     *
120
     * @param FormEvent $event
121
     */
122 8
    public function preSubmit(FormEvent $event)
123
    {
124 8
    }
125
126
    /**
127
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
128
     *
129
     * @param FormEvent $event
130
     */
131 4
    public function onSubmit(FormEvent $event)
132
    {
133 4
    }
134
135
    /**
136
     * @see http://api.symfony.com/4.0/Symfony/Component/Form/FormEvents.html
137
     *
138
     * @param FormEvent $event
139
     */
140 8
    public function postSubmit(FormEvent $event)
141
    {
142 8
    }
143
144
    /**
145
     * Actions to process
146
     * the result processed data is given to payload
147
     *
148
     * @param mixed $data
149
     */
150
    abstract public function process(&$data);
151
152
    /**
153
     * The payload object or array matching the GraphQL definition
154
     *
155
     * @param mixed                   $data        normalized data, its the input data processed by the form
156
     * @param ConstraintViolationList $violations  violations returned by the form validation process
157
     * @param array                   $inputSource the original submitted data in array
158
     *
159
     * @return mixed
160
     */
161
    abstract public function returnPayload($data, ConstraintViolationList $violations, $inputSource);
162
163
    /**
164
     * @param mixed $data
165
     *
166
     * @return FormBuilderInterface|null
167
     */
168
    public function createDefinitionForm($data): ?FormBuilderInterface
169 8
    {
170
        if (!$this->context->getDefinition()->hasMeta('form')) {
171 8
            return null;
172
        }
173
174
        $formConfig = $this->context->getDefinition()->getMeta('form') ?? [];
175 8
        $formType = $formConfig['type'] ?? null;
176 8
        if (!$formConfig || !$formType) {
177 8
            throw new \RuntimeException(sprintf('Can`t find a valid form for %s', $this->context->getDefinition()->getName()));
178
        }
179
180
        $options = [
181
            'allow_extra_fields' => true,
182 8
        ];
183
184
        if ($this->container->hasParameter('form.type_extension.csrf.enabled')
185 8
            && $this->container->getParameter('form.type_extension.csrf.enabled')) {
186 8
            $options['csrf_protection'] = false;
187 8
        }
188
189
        $options = array_merge($options, $formConfig['options'] ?? []);
190 8
191
        $form = $this->createFormBuilder($formType, $data, $options);
192 8
        $viewTransformer = new DataWithIdToNodeTransformer($this->getManager(), $this->context->getEndpoint());
193
        $form->addViewTransformer($viewTransformer);
194
195
        return $form;
196 8
    }
197 8
198 8
    /**
199
     * @param FormInterface           $form
200 8
     * @param ConstraintViolationList $violations
201
     * @param null|string             $parentName
202
     */
203
    public function extractFormErrors(FormInterface $form, ConstraintViolationList $violations, ?string $parentName = null)
204
    {
205
        $errors = $form->getErrors(true);
206
        foreach ($errors as $error) {
207
            $violation = new ConstraintViolation();
208 8
            $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

208
            $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...
209
            $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

209
            $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...
210 8
            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

210
            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...
211 8
                $violation->addParameter($key, $value);
212 1
            }
213
214 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

214
            /** @scrutinizer ignore-call */ 
215
            $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...
215 1
            if ($cause instanceof SymfonyConstraintViolation) {
216 1
                $violation->setCode($cause->getCode());
217
                $violation->setInvalidValue($cause->getInvalidValue());
218 1
                $violation->setPlural($cause->getPlural());
219 1
220
                $path = $this->publicPropertyPath($form, $cause->getPropertyPath());
221
                if ($path) {
222 1
                    $violation->setPropertyPath($path);
223 1
                }
224 1
            }
225 1
226 1
            $violations->addViolation($violation);
227
        }
228
    }
229 1
230 1
    /**
231 1
     * Convert internal validation property path to the public one,
232 1
     * required when use `property_path` in the form
233 1
     *
234
     * @param FormInterface $form
235
     * @param string        $path
236 1
     *
237
     * @return string
238 8
     */
239 8
    private function publicPropertyPath(FormInterface $form, $path)
240 8
    {
241
        if (strpos($path, '.') !== false) {
242
            $pathArray = explode('.', $path);
243 8
        } else {
244 8
            $pathArray = [$path];
245
        }
246
        if ($pathArray[0] === 'data') {
247 8
            array_shift($pathArray);
248
        }
249
250 8
        $contextForm = $form;
251
        foreach ($pathArray as &$propName) {
252
            $index = null;
253
            if (preg_match('/(\w+)(\[\d+\])$/', $propName, $matches)) {
254
                list(, $propName, $index) = $matches;
255
            }
256
            if (!$contextForm->has($propName)) {
257
                foreach ($contextForm->all() as $child) {
258
                    if ($child->getConfig()->getOption('property_path') === $propName) {
259
                        $propName = $child->getName();
260
                    }
261
                }
262
            }
263
            if ($index) {
264
                $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

264
                $propName = sprintf('%s%s', $propName, /** @scrutinizer ignore-type */ $index);
Loading history...
265
            }
266
        }
267
        unset($propName);
268
269
        return implode('.', $pathArray);
270
    }
271
}
272