Completed
Push — master ( 1f865b...981fed )
by Alexandr
14:47
created

ResolveValidator::assertValidArguments()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12.0585

Importance

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