Completed
Pull Request — master (#7)
by Yonel Ceruto
09:06 queued 03:33
created

MutationDeleteAnnotationParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 42
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 29 5
A supports() 0 3 1
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\Registry\Endpoint;
15
use Ynlo\GraphQLBundle\Form\Node\NodeDeleteInput;
16
use Ynlo\GraphQLBundle\Model\DeleteNodePayload;
17
use Ynlo\GraphQLBundle\Model\NodeInterface;
18
use Ynlo\GraphQLBundle\Mutation\DeleteNode;
19
use Ynlo\GraphQLBundle\Util\ClassUtils;
20
21
class MutationDeleteAnnotationParser extends MutationAnnotationParser
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 22
    public function supports($annotation): bool
27
    {
28 22
        return $annotation instanceof Annotation\MutationDelete;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 22
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
35
    {
36 22
        if (!$endpoint->hasTypeForClass($refClass->getName())) {
37
            throw new \RuntimeException(sprintf('Can\'t apply Delete operation to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName()));
38
        }
39
40 22
        if (!$refClass->implementsInterface(NodeInterface::class)) {
41
            throw new \RuntimeException(sprintf('Can\'t apply Delete operation to "%s", CRUD operations can only be applied to nodes. You are implementing NodeInterface in this class?', $refClass->getName()));
42
        }
43
44 22
        $definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName()));
45 22
        $bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName());
46
47
        /** @var Annotation\MutationDelete $annotation */
48 22
        $annotation->name = $annotation->name ?? 'delete'.ucfirst($definition->getName());
49 22
        $annotation->payload = $annotation->payload ?? null;
50 22
        if (!$annotation->payload) {
51 22
            $annotation->payload = DeleteNodePayload::class;
52
        }
53 22
        $annotation->node = $annotation->node ?? $definition->getName();
54 22
        $annotation->options = array_merge(['form' => ['type' => NodeDeleteInput::class]], $annotation->options);
55 22
        $resolverReflection = new \ReflectionClass(DeleteNode::class);
56
57 22
        $resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name);
58 22
        if (class_exists($resolver)) {
59
            $annotation->resolver = $resolver;
60
        }
61
62 22
        parent::parse($annotation, $resolverReflection, $endpoint);
63 22
    }
64
}
65