MutationAddUpdateAnnotationParser::parse()   F
last analyzed

Complexity

Conditions 18
Paths 242

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 18.5459

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 71
ccs 37
cts 42
cp 0.881
rs 3.5083
cc 18
nc 242
nop 3
crap 18.5459

How to fix   Long Method    Complexity   

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\ObjectDefinitionInterface;
15
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
16
use Ynlo\GraphQLBundle\Model\AddNodePayload;
17
use Ynlo\GraphQLBundle\Model\NodeInterface;
18
use Ynlo\GraphQLBundle\Model\UpdateNodePayload;
19
use Ynlo\GraphQLBundle\Mutation\AddNode;
20
use Ynlo\GraphQLBundle\Mutation\UpdateNode;
21
use Ynlo\GraphQLBundle\Util\ClassUtils;
22
23
class MutationAddUpdateAnnotationParser extends MutationAnnotationParser
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 42
    public function supports($annotation): bool
29
    {
30 42
        return ($annotation instanceof Annotation\MutationAdd || $annotation instanceof Annotation\MutationUpdate);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @param Annotation\MutationAdd $annotation
37
     */
38 11
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
39
    {
40 11
        if (!$endpoint->hasTypeForClass($refClass->getName())) {
41 1
            throw new \RuntimeException(sprintf('Can\'t apply Add/Update operation to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName()));
42
        }
43
44 10
        if (!$refClass->implementsInterface(NodeInterface::class)) {
45 1
            throw new \RuntimeException(sprintf('Can\'t apply Add/Update operation to "%s", CRUD operations can only be applied to nodes. You are implementing "%s" in this class?', $refClass->getName(), NodeInterface::class));
46
        }
47
48 9
        $definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName()));
49 9
        $bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName());
50
51 9
        $actionPrefix = $annotation instanceof Annotation\MutationAdd ? 'add' : 'update';
0 ignored issues
show
introduced by
$annotation is always a sub-type of Ynlo\GraphQLBundle\Annotation\MutationAdd.
Loading history...
52 9
        $annotation->name = $annotation->name ?? $actionPrefix.ucfirst($definition->getName());
53 9
        $annotation->payload = $annotation->payload ?? null;
54 9
        if (!$annotation->payload) {
55
            //deep cloning
56
            /** @var ObjectDefinitionInterface $payload */
57 9
            $payloadClass = $annotation instanceof Annotation\MutationAdd ? AddNodePayload::class : UpdateNodePayload::class;
0 ignored issues
show
introduced by
$annotation is always a sub-type of Ynlo\GraphQLBundle\Annotation\MutationAdd.
Loading history...
58 9
            $payload = unserialize(serialize($endpoint->getType($payloadClass)), ['allowed_classes' => true]);
59 9
            $payload->setName(ucfirst($annotation->name.'Payload'));
60
61 9
            if (!$endpoint->hasType($payload->getName())) {
62 9
                $payload->getField('node')->setType($definition->getName());
63 9
                $endpoint->add($payload);
64
            }
65
66 9
            $annotation->payload = $payload->getName();
67
        }
68 9
        $annotation->node = $annotation->node ?? $definition->getName();
69
70 9
        if ($endpoint->hasTypeForClass($annotation->node)) {
71
            $annotation->node = $endpoint->getTypeForClass($annotation->node);
72
        }
73
74 9
        $formType = null;
75 9
        $options = [];
76 9
        $generalForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->node, 'Input');
77 9
        $specificForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->name, 'Input');
78 9
        if (class_exists($specificForm)) {
79 7
            $formType = $specificForm;
80 9
        } elseif (class_exists($generalForm)) {
81 9
            $formType = $generalForm;
82 9
            $options['operation'] = $annotation->name;
83
84
            //in case the form has activated namespace operation alias
85
            //the operation name will be the alias
86 9
            foreach ($annotation->options as $name => $option) {
87
                if ($option instanceof Annotation\Plugin\Namespaces && $option->alias) {
88
                    $options['operation'] = $option->alias;
89
90
                }
91
92
                //support for BC using array
93
                if (\is_array($option) && 'namespace' === $name && $option['alias'] ?? false) {
94
                    $options['operation'] = $option->alias;
95
                }
96
            }
97
        }
98
99 9
        $annotation->options = array_merge(['form' => ['type' => $formType, 'options' => $options]], $annotation->options);
100 9
        $resolver = $annotation instanceof Annotation\MutationAdd ? AddNode::class : UpdateNode::class;
0 ignored issues
show
introduced by
$annotation is always a sub-type of Ynlo\GraphQLBundle\Annotation\MutationAdd.
Loading history...
101 9
        $resolverReflection = new \ReflectionClass($resolver);
102
103 9
        $resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name);
104 9
        if (class_exists($resolver)) {
105 8
            $annotation->resolver = $resolver;
106
        }
107
108 9
        parent::parse($annotation, $resolverReflection, $endpoint);
0 ignored issues
show
Bug introduced by
$annotation of type Ynlo\GraphQLBundle\Annotation\MutationAdd is incompatible with the type Ynlo\GraphQLBundle\Annotation\Mutation expected by parameter $annotation of Ynlo\GraphQLBundle\Defin...notationParser::parse(). ( Ignorable by Annotation )

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

108
        parent::parse(/** @scrutinizer ignore-type */ $annotation, $resolverReflection, $endpoint);
Loading history...
109 9
    }
110
}
111