Completed
Push — master ( 911cdf...67ba27 )
by Alexandr
02:54
created

ResolveValidator::assertValidArguments()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12.0585

Importance

Changes 5
Bugs 1 Features 1
Metric Value
dl 0
loc 39
ccs 25
cts 27
cp 0.9259
rs 5.1612
c 5
b 1
f 1
cc 12
eloc 23
nc 28
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 63
    public function assetTypeHasField(AbstractType $objectType, AstFieldInterface $ast)
26
    {
27
        /** @var AbstractObjectType $objectType */
28 63
        if (!(TypeService::isObjectType($objectType) || TypeService::isInputObjectType($objectType)) || !$objectType->hasField($ast->getName())) {
29
            $availableFieldNames = implode(', ', array_map(function (FieldInterface $field) {
30 3
                return sprintf('"%s"', $field->getName());
31 3
            }, $objectType->getFields()));
32 3
            throw new ResolveException(sprintf('Field "%s" not found in type "%s". Available fields are: %s', $ast->getName(), $objectType->getNamedType()->getName(), $availableFieldNames), $ast->getLocation());
33
        }
34 63
    }
35
36
    public function assertValidArguments(FieldInterface $field, AstFieldInterface $query, Request $request)
37
    {
38 62
        $requiredArguments = array_filter($field->getArguments(), function (InputField $argument) {
39 47
            return $argument->getType()->getKind() === TypeMap::KIND_NON_NULL;
40 62
        });
41
42 62
        foreach ($query->getArguments() as $astArgument) {
43 37
            if (!$field->hasArgument($astArgument->getName())) {
44 1
                throw new ResolveException(sprintf('Unknown argument "%s" on field "%s"', $astArgument->getName(), $field->getName()), $astArgument->getLocation());
45
            }
46
47 37
            $argument     = $field->getArgument($astArgument->getName());
48 37
            $argumentType = $argument->getType()->getNullableType();
49
50 37
            switch ($argumentType->getKind()) {
51 37
                case TypeMap::KIND_ENUM:
52 37
                case TypeMap::KIND_SCALAR:
53 37
                case TypeMap::KIND_INPUT_OBJECT:
54 37
                case TypeMap::KIND_LIST:
55 37
                    if (!$argument->getType()->isValidValue($astArgument->getValue())) {
56 6
                        $error = $argument->getType()->getValidationError($astArgument->getValue()) ?: '(no details available)';
57 6
                        throw new ResolveException(sprintf('Not valid type for argument "%s" in query "%s": %s', $astArgument->getName(), $field->getName(), $error), $astArgument->getLocation());
58
                    }
59
60 34
                    break;
61
62
                default:
63
                    throw new ResolveException(sprintf('Invalid argument type "%s"', $argumentType->getName()));
64 34
            }
65
66 34
            if (array_key_exists($astArgument->getName(), $requiredArguments) || $argument->getConfig()->get('defaultValue') !== null) {
67 11
                unset($requiredArguments[$astArgument->getName()]);
68 11
            }
69 59
        }
70
71 59
        if (count($requiredArguments)) {
72 3
            throw new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName()));
73
        }
74 57
    }
75
76 57
    public function assertValidResolvedValueForField(FieldInterface $field, $resolvedValue)
77
    {
78 57
        if (null === $resolvedValue && $field->getType()->getKind() === TypeMap::KIND_NON_NULL) {
79 4
            throw new ResolveException(sprintf('Cannot return null for non-nullable field "%s"', $field->getName()));
80
        }
81
82 55
        $nullableFieldType = $field->getType()->getNullableType();
83 55
        if (!$nullableFieldType->isValidValue($resolvedValue)) {
84 3
            $error = $nullableFieldType->getValidationError($resolvedValue) ?: '(no details available)';
85 3
            throw new ResolveException(sprintf('Not valid resolved type for field "%s": %s', $field->getName(),
86 3
                $error));
87
        }
88 54
    }
89
90 6
    public function assertTypeImplementsInterface(AbstractType $type, AbstractInterfaceType $interface)
91
    {
92 6
        if ($type instanceof AbstractObjectType) {
93 6
            foreach ($type->getInterfaces() as $typeInterface) {
94 6
                if ($typeInterface->getName() === $interface->getName()) {
95 6
                    return;
96
                }
97
            }
98
        }
99
100
        throw new ResolveException(sprintf('Type "%s" does not implement "%s"', $type->getName(), $interface->getName()));
101
    }
102
103 1
    public function assertTypeInUnionTypes(AbstractType $type, AbstractUnionType $unionType)
104
    {
105 1
        foreach ($unionType->getTypes() as $unionTypeItem) {
106 1
            if ($unionTypeItem->getName() === $type->getName()) {
107 1
                return;
108
            }
109 1
        }
110
111 1
        throw new ResolveException(sprintf('Type "%s" not exist in types of "%s"', $type->getName(), $unionType->getName()));
112
    }
113
}
114