Passed
Pull Request — master (#7)
by Yonel Ceruto
07:59
created

MutationAnnotationParser::parse()   F

Complexity

Conditions 16
Paths 442

Size

Total Lines 85
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 28.9251

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 29
cts 46
cp 0.6304
rs 3.485
c 0
b 0
f 0
cc 16
eloc 52
nc 442
nop 3
crap 28.9251

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 Symfony\Component\Form\FormFactory;
14
use Ynlo\GraphQLBundle\Annotation;
15
use Ynlo\GraphQLBundle\Definition\MutationDefinition;
16
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
17
use Ynlo\GraphQLBundle\Util\ClassUtils;
18
19
/**
20
 * Parse mutation annotation to fetch definitions
21
 */
22
class MutationAnnotationParser extends QueryAnnotationParser
23
{
24
    protected $formFactory;
25
26 22
    public function __construct(FormFactory $formFactory)
27
    {
28 22
        $this->formFactory = $formFactory;
29 22
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 22
    public function supports($annotation): bool
35
    {
36 22
        return $annotation instanceof Annotation\Mutation;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42 22
     * @param Annotation\Mutation $annotation
43
     */
44
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
45
    {
46 22
        if (!preg_match('/Bundle\\\\Mutation\\\\/', $refClass->getName())) {
47
            $error = sprintf(
48
                'Annotation "@Mutation" in the class "%s" is not valid, 
49
            mutations can only be applied to classes inside "...Bundle\Mutation\..."',
50
                $refClass->getName()
51
            );
52
            throw new \RuntimeException($error);
53
        }
54
55 22
        if (!$annotation->resolver && !$refClass->hasMethod('__invoke')) {
56
            $error = sprintf(
57
                'The class "%s" should have a method "__invoke" to process the mutation.',
58
                $refClass->getName()
59
            );
60
            throw new \RuntimeException($error);
61
        }
62
63 22
        $mutation = new MutationDefinition();
64
65 22
        if ($annotation->name) {
66 22
            $mutation->setName($annotation->name);
67
        } else {
68 22
            $mutation->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
69
        }
70
71 22
        $endpoint->addMutation($mutation);
72
73 22
        if (!$annotation->payload) {
74 22
            if (class_exists($refClass->getName().'Payload')) {
75 22
                $annotation->payload = $refClass->getName().'Payload';
76 22
                if (!$endpoint->hasTypeForClass($annotation->payload)) {
77
                    $error = sprintf(
78
                        'The payload "%s" exist but does not exist a valid GraphQL type, is missing ObjectType annotation?',
79
                        $annotation->payload
80
                    );
81
                    throw new \RuntimeException($error);
82
                }
83
            }
84
        }
85
86 22
        $mutation->setType($annotation->payload);
87
88 22
        if (!$mutation->getType()) {
89
            $error = sprintf(
90
                'The mutation "%s" does not have a valid payload,
91
                 create a file called %sPayload or specify a payload.',
92
                $mutation->getName(),
93
                $refClass->getName()
94
            );
95
            throw new \RuntimeException($error);
96
        }
97
98 22
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
99 22
        foreach ($argAnnotations as $argAnnotation) {
100 22
            if ($argAnnotation instanceof Annotation\Argument) {
101 22
                $this->resolveArgument($mutation, $argAnnotation);
102
            }
103
        }
104
105 22
        if ($annotation->node) {
106 22
            $mutation->setNode($annotation->node);
107
        } else {
108 22
            if ($node = ClassUtils::getNodeFromClass($refClass->getName())) {
109 22
                $mutation->setNode($node);
110
            }
111
        }
112
113 22
        $mutation->setResolver($annotation->resolver ?? $refClass->getName());
114 22
        $mutation->setDeprecationReason($annotation->deprecationReason);
115 22
        $mutation->setDescription($annotation->description);
116
        $mutation->setRoles((array) $annotation->roles);
117
118 22
        //enable form auto-loaded by default
119 22
        if (!isset($annotation->options['form'])) {
120
            $annotation->options['form'] = true;
121
        }
122 22
123 22
        if ($annotation->roles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->roles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124
            $annotation->options['roles'] = (array) $annotation->roles;
125 22
        }
126
127
        foreach ($annotation->options as $option => $value) {
128
            $mutation->setMeta($option, $value);
129
        }
130
    }
131
}
132