Completed
Push — master ( f9f0ef...24a695 )
by Portey
05:15
created

ResolveValidator::validateArguments()   C

Complexity

Conditions 11
Paths 31

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 15.1269

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 57
ccs 25
cts 37
cp 0.6757
rs 6.4824
cc 11
eloc 30
nc 31
nop 3
crap 15.1269

How to fix   Long Method    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: 01.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Validator\ResolveValidator;
9
10
11
use Youshido\GraphQL\Parser\Ast\Argument;
12
use Youshido\GraphQL\Parser\Value\Literal;
13
use Youshido\GraphQL\Type\Field\InputField;
14
use Youshido\GraphQL\Type\TypeMap;
15
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait;
16
use Youshido\GraphQL\Parser\Value\Variable;
17
use Youshido\GraphQL\Type\AbstractType;
18
use Youshido\GraphQL\Validator\Exception\ResolveException;
19
20
class ResolveValidator implements ResolveValidatorInterface
21
{
22
23
    use ErrorContainerTrait;
24
25
    /**
26
     * @inheritdoc
27
     */
28 18
    public function validateArguments($field, $query, $request)
29
    {
30
        $requiredArguments = array_filter($field->getConfig()->getArguments(), function (InputField $argument) {
31 9
            return $argument->getConfig()->get('required');
32 18
        });
33
34 18
        $withDefaultArguments = array_filter($field->getConfig()->getArguments(), function (InputField $argument) {
35 9
            return $argument->getConfig()->get('default') !== null;
36 18
        });
37
38 18
        foreach ($query->getArguments() as $argument) {
39 5
            if (!array_key_exists($argument->getName(), $field->getConfig()->getArguments())) {
40
                $this->addError(new ResolveException(sprintf('Unknown argument "%s" on field "%s".', $argument->getName(), $field->getName())));
41
42
                return false;
43
            }
44
45
            /** @var AbstractType $argumentType */
46 5
            $argumentType = $field->getConfig()->getArgument($argument->getName())->getType();
47 5
            if ($argument->getValue() instanceof Variable) {
48 1
                if ($request->hasVariable($argument->getValue()->getName())) {
49 1
                    $argument->getValue()->setValue($request->getVariable($argument->getValue()->getName()));
50 1
                } else {
51
                    $this->addError(new ResolveException(sprintf('Variable "%s" not exist for query "%s"', $argument->getName(), $field->getName())));
52
53
                    return false;
54
                }
55 1
            }
56
57 5
            if (!$argumentType->isValidValue($argumentType->parseValue($argument->getValue()->getValue()))) {
58
                $this->addError(new ResolveException(sprintf('Not valid type for argument "%s" in query "%s"', $argument->getName(), $field->getName())));
59
60
                return false;
61
            }
62
63 5
            if (array_key_exists($argument->getName(), $requiredArguments)) {
64 2
                unset($requiredArguments[$argument->getName()]);
65 2
            }
66 5
            if (array_key_exists($argument->getName(), $withDefaultArguments)) {
67
                unset($withDefaultArguments[$argument->getName()]);
68
            }
69 18
        }
70
71 18
        if (count($requiredArguments)) {
72 1
            $this->addError(new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName())));
73
74 1
            return false;
75
        }
76
77 17
        if (count($withDefaultArguments)) {
78
            foreach ($withDefaultArguments as $name => $argument) {
79
                $query->addArgument(new Argument($name, new Literal( $argument->getConfig()->get('default'))));
80
            }
81
        }
82
83 17
        return true;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 17
    public function validateResolvedValue($value, $kind)
90
    {
91
        switch ($kind) {
92 17
            case TypeMap::KIND_OBJECT:
93 17
            case TypeMap::KIND_INPUT_OBJECT:
94 17
            case TypeMap::KIND_INTERFACE:
95 15
                return is_object($value) || is_null($value) || is_array($value);
96 8
            case TypeMap::KIND_LIST:
97 8
                return is_null($value)|| is_array($value) || (is_object($value) && in_array('IteratorAggregate', class_implements($value)));
98
        }
99
100
        return false;
101
    }
102
103
}