Completed
Push — master ( 300eda...15c856 )
by Rafael
09:26
created

MutationAnnotationParser::parse()   F

Complexity

Conditions 16
Paths 442

Size

Total Lines 90
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 0
cts 70
cp 0
rs 3.485
c 0
b 0
f 0
cc 16
eloc 55
nc 442
nop 3
crap 272

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
use Ynlo\GraphQLBundle\Util\TypeUtil;
19
20
/**
21
 * Parse mutation annotation to fetch definitions
22
 */
23
class MutationAnnotationParser extends QueryAnnotationParser
24
{
25
    protected $formFactory;
26
27
    public function __construct(FormFactory $formFactory)
28
    {
29
        $this->formFactory = $formFactory;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function supports($annotation): bool
36
    {
37
        return $annotation instanceof Annotation\Mutation;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @param Annotation\Mutation $annotation
44
     */
45
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
46
    {
47
        /** @var Annotation\Mutation $annotation */
48
49
        if (!preg_match('/\\Mutation\\\\/', $refClass->getName())) {
50
            $error = sprintf(
51
                'Annotation "@Mutation" in the class "%s" is not valid, 
52
            mutations can only be applied to classes inside "...Bundle\Mutation\..."',
53
                $refClass->getName()
54
            );
55
            throw new \RuntimeException($error);
56
        }
57
58
        if (!$annotation->resolver && !$refClass->hasMethod('__invoke')) {
59
            $error = sprintf(
60
                'The class "%s" should have a method "__invoke" to process the mutation.',
61
                $refClass->getName()
62
            );
63
            throw new \RuntimeException($error);
64
        }
65
66
        $mutation = new MutationDefinition();
67
68
        if ($annotation->name) {
69
            $mutation->setName($annotation->name);
70
        } else {
71
            $mutation->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
72
        }
73
74
        $endpoint->addMutation($mutation);
75
76
        if (!$annotation->payload) {
77
            if (class_exists($refClass->getName().'Payload')) {
78
                $annotation->payload = $refClass->getName().'Payload';
79
                if (!$endpoint->hasTypeForClass($annotation->payload)) {
80
                    $error = sprintf(
81
                        'The payload "%s" exist but does not exist a valid GraphQL type, is missing ObjectType annotation?',
82
                        $annotation->payload
83
                    );
84
                    throw new \RuntimeException($error);
85
                }
86
            }
87
        }
88
89
        $mutation->setType(TypeUtil::normalize($annotation->payload));
90
        $mutation->setList(TypeUtil::isTypeList($annotation->payload));
91
        $mutation->setNonNullList(TypeUtil::isTypeNonNullList($annotation->payload));
92
        $mutation->setNonNull(TypeUtil::isTypeNonNull($annotation->payload));
93
94
        if (!$mutation->getType()) {
95
            $error = sprintf(
96
                'The mutation "%s" does not have a valid payload,
97
                 create a file called %sPayload or specify a payload.',
98
                $mutation->getName(),
99
                $refClass->getName()
100
            );
101
            throw new \RuntimeException($error);
102
        }
103
104
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
105
        foreach ($argAnnotations as $argAnnotation) {
106
            if ($argAnnotation instanceof Annotation\Argument) {
107
                $this->resolveArgument($mutation, $argAnnotation);
108
            }
109
        }
110
111
        if ($annotation->node) {
112
            $mutation->setNode($annotation->node);
113
        } else {
114
            if ($node = ClassUtils::getNodeFromClass($refClass->getName())) {
115
                $mutation->setNode($node);
116
            }
117
        }
118
119
        $mutation->setResolver($annotation->resolver ?? $refClass->getName());
120
        $mutation->setDeprecationReason($annotation->deprecationReason);
121
        $mutation->setDescription($annotation->description);
122
        $mutation->setRoles((array) $annotation->roles);
123
124
        //enable form auto-loaded by default
125
        if (!isset($annotation->options['form'])) {
126
            $annotation->options['form'] = true;
127
        }
128
129
        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...
130
            $annotation->options['roles'] = (array) $annotation->roles;
131
        }
132
133
        foreach ($annotation->options as $option => $value) {
134
            $mutation->setMeta($option, $value);
135
        }
136
    }
137
}
138