Completed
Pull Request — master (#133)
by Adrien
04:00
created

ResolveValidator::assertValidArguments()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12.0585

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 40
ccs 25
cts 27
cp 0.9259
rs 5.1612
c 2
b 1
f 0
cc 12
eloc 23
nc 24
nop 3
crap 12.0585

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Date: 03.11.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Validator\ResolveValidator;
9
10
11
use Youshido\GraphQL\Exception\ResolveException;
12
use Youshido\GraphQL\Execution\Request;
13
use Youshido\GraphQL\Field\FieldInterface;
14
use Youshido\GraphQL\Field\InputField;
15
use Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface as AstFieldInterface;
16
use Youshido\GraphQL\Type\AbstractType;
17
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
18
use Youshido\GraphQL\Type\Object\AbstractObjectType;
19
use Youshido\GraphQL\Type\TypeMap;
20
use Youshido\GraphQL\Type\TypeService;
21
use Youshido\GraphQL\Type\Union\AbstractUnionType;
22
23
class ResolveValidator implements ResolveValidatorInterface
24
{
25
26 57
    public function assetTypeHasField(AbstractType $objectType, AstFieldInterface $ast)
27
    {
28
        /** @var AbstractObjectType $objectType */
29 57
        if (!(TypeService::isObjectType($objectType) || TypeService::isInputObjectType($objectType)) || !$objectType->hasField($ast->getName())) {
30 3
            throw new ResolveException(sprintf('Field "%s" not found in type "%s"', $ast->getName(), $objectType->getNamedType()->getName()), $ast->getLocation());
31
        }
32 57
    }
33
34
    public function assertValidArguments(FieldInterface $field, AstFieldInterface $query, Request $request)
35
    {
36 56
        $requiredArguments = array_filter($field->getArguments(), function (InputField $argument) {
37 41
            return $argument->getType()->getKind() === TypeMap::KIND_NON_NULL;
38 56
        });
39
40 56
        foreach ($query->getArguments() as $astArgument) {
41 31
            if (!$field->hasArgument($astArgument->getName())) {
42 1
                throw new ResolveException(sprintf('Unknown argument "%s" on field "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation());
43
            }
44
45 31
            $argument     = $field->getArgument($astArgument->getName());
46 31
            $argumentType = $argument->getType()->getNullableType();
47
48 31
            switch ($argumentType->getKind()) {
49 31
                case TypeMap::KIND_ENUM:
50 31
                case TypeMap::KIND_SCALAR:
51 31
                case TypeMap::KIND_INPUT_OBJECT:
52 31
                case TypeMap::KIND_LIST:
53 31
                    if (!$argument->getType()->isValidValue($argumentType->parseValue($astArgument->getValue()))) {
54 5
                        $error = $argument->getType()->getLastError();
55 5
                        throw new ResolveException($error ? $error : sprintf('Not valid type for argument "%s" in query "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation());
56
                    }
57
58 28
                    break;
59
60
                default:
61
                    throw new ResolveException(sprintf('Invalid argument type "%s"', $argumentType->getName()));
62
63 28
            }
64
65 28
            if (array_key_exists($astArgument->getName(), $requiredArguments) || $argument->getConfig()->get('defaultValue') !== null) {
66 11
                unset($requiredArguments[$astArgument->getName()]);
67 11
            }
68 53
        }
69
70 53
        if (count($requiredArguments)) {
71 3
            throw new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName()));
72
        }
73 51
    }
74
75 51
    public function assertValidResolvedValueForField(FieldInterface $field, $resolvedValue)
76
    {
77 51
        if (null === $resolvedValue && $field->getType()->getKind() === TypeMap::KIND_NON_NULL) {
78 4
            throw new ResolveException(sprintf('Cannot return null for non-nullable field "%s"', $field->getName()));
79
        }
80
81 49
        if (!$field->getType()->getNullableType()->isValidValue($resolvedValue)) {
82 3
            throw new ResolveException(sprintf('Not valid resolved type for field "%s"', $field->getName()));
83
        }
84 48
    }
85
86 6
    public function assertTypeImplementsInterface(AbstractType $type, AbstractInterfaceType $interface)
87
    {
88 6
        if ($type instanceof AbstractObjectType) {
89 6
            foreach ($type->getInterfaces() as $typeInterface) {
90 6
                if ($typeInterface->getName() === $interface->getName()) {
91 6
                    return;
92
                }
93
            }
94
        }
95
96
        throw new ResolveException(sprintf('Type "%s" does not implement "%s"', $type->getName(), $interface->getName()));
97
    }
98
99 1
    public function assertTypeInUnionTypes(AbstractType $type, AbstractUnionType $unionType)
100
    {
101 1
        foreach ($unionType->getTypes() as $unionTypeItem) {
102 1
            if ($unionTypeItem->getName() === $type->getName()) {
103 1
                return;
104
            }
105 1
        }
106
107 1
        throw new ResolveException(sprintf('Type "%s" not exist in types of "%s"', $type->getName(), $unionType->getName()));
108
    }
109
110
}
111