Completed
Push — master ( 658b32...4b751e )
by Rafael
06:02
created

StandaloneFieldParser::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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\FieldDefinition;
15
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
16
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
17
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
18
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
19
use Ynlo\GraphQLBundle\Util\ClassUtils;
20
use Ynlo\GraphQLBundle\Util\TypeUtil;
21
22
/**
23
 * Resolve field of types queries using naming conventions
24
 */
25
class StandaloneFieldParser extends QueryAnnotationParser
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 39
    public function supports($annotation): bool
31
    {
32 39
        return $annotation instanceof Annotation\Field;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @param Annotation\Field $annotation
39
     */
40 1
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
41
    {
42 1
        $field = new FieldDefinition();
43
44 1
        if ($annotation->name) {
45
            $field->setName($annotation->name);
46
        } else {
47 1
            $field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
48
        }
49
50 1
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
51 1
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
52 1
        if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) {
53
            $error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName());
54
            throw new \RuntimeException($error);
55
        }
56
        /** @var ObjectDefinitionInterface $objectDefinition */
57 1
        $objectDefinition = $endpoint->getType($matches[1]);
58 1
        $objectDefinition->addField($field);
59
60 1
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
61 1
        foreach ($argAnnotations as $argAnnotation) {
62 1
            if ($argAnnotation instanceof Annotation\Argument) {
63 1
                $this->resolveArgument($field, $argAnnotation);
64
            }
65
        }
66
67 1
        $field->setType(TypeUtil::normalize($annotation->type));
68 1
        $field->setList(TypeUtil::isTypeList($annotation->type));
69 1
        $field->setNonNull(TypeUtil::isTypeNonNull($annotation->type));
70 1
        $field->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type));
71 1
        $field->setResolver($annotation->resolver ?? $refClass->getName());
72 1
        $field->setDeprecationReason($annotation->deprecationReason);
73 1
        $field->setDescription($annotation->description);
74 1
        $field->setComplexity($annotation->complexity);
75 1
        $field->setMaxConcurrentUsage($annotation->maxConcurrentUsage);
76
77 1
        foreach ($annotation->options as $option => $value) {
78 1
            $field->setMeta($option, $value);
79
        }
80
81 1
        if ($objectDefinition instanceof InterfaceDefinition) {
82 1
            $implementors = $objectDefinition->getImplementors();
83 1
            foreach ($implementors as $implementor) {
84 1
                $childType = $endpoint->getType($implementor);
85 1
                if ($childType instanceof FieldsAwareDefinitionInterface) {
86 1
                    $childType->addField($field);
87
                }
88
            }
89
        }
90 1
    }
91
}
92