Completed
Push — master ( 45d3b1...40b0e0 )
by Rafael
04:30
created

MutationDeleteBatchAnnotationParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 43
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

2 Methods

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