Completed
Pull Request — master (#4)
by Yonel Ceruto
08:00
created

QueryListAnnotationParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 46
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
B parse() 0 33 6
1
<?php
2
3
/*******************************************************************************
4
 *  This file is part of the GraphQL Bundle package.
5
 *
6
 *  (c) YnloUltratech <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 ******************************************************************************/
11
12
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation;
13
14
use Doctrine\Common\Util\Inflector;
15
use Ynlo\GraphQLBundle\Annotation;
16
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
17
use Ynlo\GraphQLBundle\Model\NodeInterface;
18
use Ynlo\GraphQLBundle\Query\Node\AllNodesWithPagination;
19
use Ynlo\GraphQLBundle\Util\ClassUtils;
20
21
class QueryListAnnotationParser extends QueryAnnotationParser
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 21
    public function supports($annotation): bool
27
    {
28 21
        return $annotation instanceof Annotation\QueryList;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 21
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
35
    {
36 21
        if (!$endpoint->hasTypeForClass($refClass->getName())) {
37
            throw new \RuntimeException(sprintf('Can\'t apply CRUD operations to "%s", CRUD operations can only be applied to valid GraphQL object types.', $refClass->getName()));
38
        }
39
40 21
        if (!$refClass->implementsInterface(NodeInterface::class)) {
41
            throw new \RuntimeException(sprintf('Can\'t apply CRUD operations to "%s", CRUD operations can only be applied to nodes. You are implementing NodeInterface in this class?', $refClass->getName()));
42
        }
43
44 21
        $definition = $endpoint->getType($endpoint->getTypeForClass($refClass->getName()));
45
46
        /** @var Annotation\QueryList $annotation */
47 21
        $annotation->name = $annotation->name ?? 'all'.Inflector::pluralize(ucfirst($definition->getName()));
48 21
        $annotation->type = $annotation->type ?? $definition->getName();
49 21
        $annotation->options = array_merge(['pagination' => true], $annotation->options);
50 21
        if ($annotation->limit) {
51
            if (!\is_array($annotation->options['pagination'])) {
52
                $annotation->options['pagination'] = [];
53
            }
54
            $annotation->options['pagination']['limit'] = $annotation->limit;
55
        }
56
57 21
        $bundleNamespace = ClassUtils::relatedBundleNamespace($refClass->getName());
58
59 21
        $resolver = ClassUtils::applyNamingConvention($bundleNamespace, 'Query', $definition->getName(), $annotation->name);
60 21
        if (class_exists($resolver)) {
61
            $annotation->resolver = $resolver;
62
        }
63
64 21
        $resolverReflection = new \ReflectionClass(AllNodesWithPagination::class);
65
66 21
        parent::parse($annotation, $resolverReflection, $endpoint);
67 21
    }
68
}
69