Completed
Push — master ( 186982...2975e7 )
by Rafael
05:12
created

MutationAddUpdateAnnotationParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 72
ccs 38
cts 39
cp 0.9744
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 57 12
A supports() 0 3 2
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 26
    public function supports($annotation): bool
29
    {
30 26
        return ($annotation instanceof Annotation\MutationAdd || $annotation instanceof Annotation\MutationUpdate);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @param Annotation\MutationAdd $annotation
37
     */
38 10
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
39
    {
40 10
        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 9
        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 8
        $definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName()));
49 8
        $bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName());
50
51 8
        $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 8
        $annotation->name = $annotation->name ?? $actionPrefix.ucfirst($definition->getName());
53 8
        $annotation->payload = $annotation->payload ?? null;
54 8
        if (!$annotation->payload) {
55
            //deep cloning
56
            /** @var ObjectDefinitionInterface $payload */
57 8
            $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 8
            $payload = unserialize(serialize($endpoint->getType($payloadClass)), ['allowed_classes' => true]);
59 8
            $payload->setName(ucfirst($annotation->name.'Payload'));
60
61 8
            if (!$endpoint->hasType($payload->getName())) {
62 8
                $payload->getField('node')->setType($definition->getName());
63 8
                $endpoint->add($payload);
64
            }
65
66 8
            $annotation->payload = $payload->getName();
67
        }
68 8
        $annotation->node = $annotation->node ?? $definition->getName();
69
70 8
        if ($endpoint->hasTypeForClass($annotation->node)) {
71
            $annotation->node = $endpoint->getTypeForClass($annotation->node);
72
        }
73
74 8
        $formType = true;
75 8
        $options = [];
76 8
        $generalForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->node, 'Input');
77 8
        $specificForm = ClassUtils::applyNamingConvention($bundleNamespace, 'Form\Input', $annotation->node, $annotation->name, 'Input');
78 8
        if (class_exists($specificForm)) {
79 6
            $formType = $specificForm;
80 8
        } elseif (class_exists($generalForm)) {
81 8
            $formType = $generalForm;
82 8
            $options['operation'] = $annotation->name;
83
        }
84
85 8
        $annotation->options = array_merge(['form' => ['type' => $formType, 'options' => $options]], $annotation->options);
86 8
        $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...
87 8
        $resolverReflection = new \ReflectionClass($resolver);
88
89 8
        $resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name);
90 8
        if (class_exists($resolver)) {
91 7
            $annotation->resolver = $resolver;
92
        }
93
94 8
        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

94
        parent::parse(/** @scrutinizer ignore-type */ $annotation, $resolverReflection, $endpoint);
Loading history...
95 8
    }
96
}
97