Completed
Pull Request — master (#8)
by Yonel Ceruto
15:14 queued 06:25
created

MutationDeleteAnnotationParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
B parse() 0 28 5
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
     * @param Annotation\MutationDelete $annotation
35
     */
36 22
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
37
    {
38 22
        if (!$endpoint->hasTypeForClass($refClass->getName())) {
39
            throw new \RuntimeException(sprintf('Can\'t apply Delete operation to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName()));
40
        }
41
42 22
        if (!$refClass->implementsInterface(NodeInterface::class)) {
43
            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()));
44
        }
45
46 22
        $definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName()));
47 22
        $bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName());
48
49 22
        $annotation->name = $annotation->name ?? 'delete'.ucfirst($definition->getName());
50 22
        $annotation->payload = $annotation->payload ?? null;
51 22
        if (!$annotation->payload) {
52 22
            $annotation->payload = DeleteNodePayload::class;
53
        }
54 22
        $annotation->node = $annotation->node ?? $definition->getName();
55 22
        $annotation->options = array_merge(['form' => ['type' => NodeDeleteInput::class]], $annotation->options);
56 22
        $resolverReflection = new \ReflectionClass(DeleteNode::class);
57
58 22
        $resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Mutation', $definition->getName(), $annotation->name);
59 22
        if (class_exists($resolver)) {
60
            $annotation->resolver = $resolver;
61
        }
62
63 22
        parent::parse($annotation, $resolverReflection, $endpoint);
64 22
    }
65
}
66