Completed
Pull Request — master (#4)
by Yonel Ceruto
07:42
created

MutationUpdateAnnotationParser::parse()   B

Complexity

Conditions 9
Paths 38

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9.707

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 27
cts 34
cp 0.7941
rs 7.2446
c 0
b 0
f 0
cc 9
eloc 33
nc 38
nop 3
crap 9.707

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Definition\Loader\Annotation;
12
13
use Ynlo\GraphQLBundle\Annotation;
14
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
15
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
16
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
17
use Ynlo\GraphQLBundle\Model\NodeInterface;
18
use Ynlo\GraphQLBundle\Model\UpdateNodePayload;
19
use Ynlo\GraphQLBundle\Mutation\UpdateNode;
20
use Ynlo\GraphQLBundle\Util\ClassUtils;
21
22
class MutationUpdateAnnotationParser extends MutationAnnotationParser
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 21
    public function supports($annotation): bool
28
    {
29 21
        return $annotation instanceof Annotation\MutationUpdate;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 21
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
36
    {
37 21
        if (!$endpoint->hasTypeForClass($refClass->getName())) {
38
            throw new \RuntimeException(sprintf('Can\'t apply Update operation to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName()));
39
        }
40
41 21
        if (!$refClass->implementsInterface(NodeInterface::class)) {
42
            throw new \RuntimeException(sprintf('Can\'t apply Update operation to "%s", CRUD operations can only be applied to nodes. You are implementing NodeInterface in this class?', $refClass->getName()));
43
        }
44
45 21
        $definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName()));
46 21
        $bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName());
47
48
        /** @var Annotation\MutationUpdate $annotation */
49 21
        $annotation->name = $annotation->name ?? 'update'.ucfirst($definition->getName());
50 21
        $annotation->payload = $annotation->payload ?? null;
51 21
        if (!$annotation->payload) {
52
            //deep cloning
53
            /** @var ObjectDefinitionInterface $payload */
54 21
            $payload = unserialize(serialize($endpoint->getType(UpdateNodePayload::class)), [DefinitionInterface::class]);
55 21
            $payload->setName(ucfirst($annotation->name.'Payload'));
56
57 21
            if (!$endpoint->hasType($payload->getName())) {
58 21
                $payload->getField('node')->setType($definition->getName());
59 21
                $endpoint->add($payload);
60
            }
61
62 21
            $annotation->payload = $payload->getName();
63
        }
64 21
        $annotation->node = $annotation->node ?? $definition->getName();
65
66 21
        if ($endpoint->hasTypeForClass($annotation->node)) {
67
            $annotation->node = $endpoint->getTypeForClass($annotation->node);
68
        }
69
70 21
        $formType = true;
71 21
        $options = [];
72 21
        $generalForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->node, 'Input');
73 21
        $specificForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->name, 'Input');
74 21
        if (class_exists($specificForm)) {
75 21
            $formType = $specificForm;
76
        } elseif (class_exists($generalForm)) {
77
            $formType = $generalForm;
78
            $options['operation'] = $annotation->name;
79
        }
80
81 21
        $annotation->options = array_merge(['form' => ['type' => $formType, 'options' => $options]], $annotation->options);
82 21
        $resolverReflection = new \ReflectionClass(UpdateNode::class);
83
84 21
        $resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name);
85 21
        if (class_exists($resolver)) {
86
            $annotation->resolver = $resolver;
87
        }
88
89 21
        parent::parse($annotation, $resolverReflection, $endpoint);
90 21
    }
91
}
92