Completed
Push — master ( 998494...8e3b13 )
by Rafael
10s
created

QueryListAnnotationParser::parse()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.1338

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 13
cts 19
cp 0.6842
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 8
nop 3
crap 7.1338
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