Completed
Pull Request — master (#153)
by Portey
04:20
created

ResolveValidator::assertValidArguments()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12.0117

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 40
ccs 22
cts 23
cp 0.9565
rs 5.1612
cc 12
eloc 23
nc 24
nop 3
crap 12.0117

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 59
    public function assetTypeHasField(AbstractType $objectType, AstFieldInterface $ast)
27
    {
28
        /** @var AbstractObjectType $objectType */
29 59
        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 59
    }
33
34
    public function assertValidArguments(FieldInterface $field, AstFieldInterface $query, Request $request)
35
    {
36 58
        $requiredArguments = array_filter($field->getArguments(), function (InputField $argument) {
37 43
            return $argument->getType()->getKind() === TypeMap::KIND_NON_NULL;
38 58
        });
39
40 58
        foreach ($query->getArguments() as $astArgument) {
41 33
            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 33
            $argument     = $field->getArgument($astArgument->getName());
46 33
            $argumentType = $argument->getType()->getNullableType();
47
48 33
            switch ($argumentType->getKind()) {
49 33
                case TypeMap::KIND_ENUM:
50 31
                case TypeMap::KIND_SCALAR:
51 12
                case TypeMap::KIND_INPUT_OBJECT:
52 7
                case TypeMap::KIND_LIST:
53 33
                    if (!$argument->getType()->isValidValue($astArgument->getValue())) {
54 6
                        $error = $argument->getType()->getLastError();
55 6
                        throw new ResolveException($error ? $error : sprintf('Not valid type for argument "%s" in query "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation());
56
                    }
57
58 30
                    break;
59
60
                default:
61
                    throw new ResolveException(sprintf('Invalid argument type "%s"', $argumentType->getName()));
62
63
            }
64
65 30
            if (array_key_exists($astArgument->getName(), $requiredArguments) || $argument->getConfig()->get('defaultValue') !== null) {
66 30
                unset($requiredArguments[$astArgument->getName()]);
67
            }
68
        }
69
70 55
        if (count($requiredArguments)) {
71 3
            throw new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName()));
72
        }
73 53
    }
74
75 53
    public function assertValidResolvedValueForField(FieldInterface $field, $resolvedValue)
76
    {
77 53
        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 51
        if (!$field->getType()->getNullableType()->isValidValue($resolvedValue)) {
82 3
            throw new ResolveException(sprintf('Not valid resolved type for field "%s"', $field->getName()));
83
        }
84 50
    }
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
        }
106
107 1
        throw new ResolveException(sprintf('Type "%s" not exist in types of "%s"', $type->getName(), $unionType->getName()));
108
    }
109
110
}
111