Completed
Push — master ( 150b90...d63187 )
by Portey
04:50
created

ResolveValidator::assertValidArguments()   C

Complexity

Conditions 11
Paths 24

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11.0113

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 39
ccs 21
cts 22
cp 0.9545
rs 5.2653
c 1
b 1
f 0
cc 11
eloc 22
nc 24
nop 3
crap 11.0113

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