Passed
Push — master ( c39fb6...9e9a0d )
by Rafael
04:42
created

StandaloneFieldParser::parse()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 36
Code Lines 23

Duplication

Lines 5
Ratio 13.89 %

Code Coverage

Tests 20
CRAP Score 7.1086

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 14
nop 3
dl 5
loc 36
ccs 20
cts 23
cp 0.8696
crap 7.1086
rs 6.7272
c 0
b 0
f 0
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\Registry\Endpoint;
16
use Ynlo\GraphQLBundle\Util\ClassUtils;
17
use Ynlo\GraphQLBundle\Util\TypeUtil;
18
19
/**
20
 * Resolve field of types queries using naming conventions
21
 */
22
class StandaloneFieldParser extends QueryAnnotationParser
23
{
24
    use AnnotationReaderAwareTrait;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function supports($annotation): bool
30
    {
31 1
        return $annotation instanceof Annotation\Field;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint)
38
    {
39
        /** @var Annotation\Field $annotation */
40
41 1
        $field = new FieldDefinition();
42
43 1 View Code Duplication
        if ($annotation->name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            $field->setName($annotation->name);
45
        } else {
46 1
            $field->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName())));
47
        }
48
49 1
        $objectType = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $objectType is dead and can be removed.
Loading history...
50 1
        preg_match('/(\w+)\\\\Field\\\\(\w+)$/', $refClass->getName(), $matches);
51 1
        if (!isset($matches[1]) || !$endpoint->hasType($matches[1])) {
52
            $error = sprintf('Can`t resolve a valid object type for field "%s"', $refClass->getName());
53
            throw new \RuntimeException($error);
54
        }
55 1
        $objectDefinition = $endpoint->getType($matches[1]);
56 1
        $objectDefinition->addField($field);
57
58 1
        $argAnnotations = $this->reader->getClassAnnotations($refClass);
59 1
        foreach ($argAnnotations as $argAnnotation) {
60 1
            if ($argAnnotation instanceof Annotation\Argument) {
61 1
                $this->resolveArgument($field, $argAnnotation);
62
            }
63
        }
64
65 1
        $field->setType(TypeUtil::normalize($annotation->type));
66 1
        $field->setList(TypeUtil::isTypeList($annotation->type));
67 1
        $field->setResolver($annotation->resolver ?? $refClass->getName());
0 ignored issues
show
Bug introduced by
The property resolver does not seem to exist on Ynlo\GraphQLBundle\Annotation\Field.
Loading history...
68 1
        $field->setDeprecationReason($annotation->deprecationReason);
69 1
        $field->setDescription($annotation->description);
70
71 1
        foreach ($annotation->options as $option => $value) {
72 1
            $field->setMeta($option, $value);
73
        }
74 1
    }
75
}
76