Completed
Push — master ( b8b70e...552805 )
by Portey
03:44
created

ResolveValidator::validateResolvedValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.1158

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 8.8571
cc 5
eloc 7
nc 5
nop 2
crap 5.1158
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\Type\TypeMap;
12
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait;
13
use Youshido\GraphQL\Type\Field\Field;
14
use Youshido\GraphQL\Parser\Value\Variable;
15
use Youshido\GraphQL\Type\AbstractType;
16
use Youshido\GraphQL\Validator\Exception\ResolveException;
17
18
class ResolveValidator implements ResolveValidatorInterface
19
{
20
21
    use ErrorContainerTrait;
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function validateArguments($queryType, $query, $request)
27
    {
28 3
        $requiredArguments = array_filter($queryType->getConfig()->getArguments(), function (Field $argument) {
29 1
            return $argument->getConfig()->isRequired();
30 3
        });
31
32 3
        foreach ($query->getArguments() as $argument) {
33 1
            if (!array_key_exists($argument->getName(), $queryType->getConfig()->getArguments())) {
34
                $this->addError(new ResolveException(sprintf('Unknown argument "%s" on field "%s".', $argument->getName(), $queryType->getName())));
35
36
                return false;
37
            }
38
39
            /** @var AbstractType $argumentType */
40 1
            $argumentType = $queryType->getConfig()->getArgument($argument->getName())->getType();
41 1
            if ($argument->getValue() instanceof Variable) {
42
                if ($request->hasVariable($argument->getName())) {
43
                    $argument->getValue()->setValue($request->getVariable($argument->getName()));
44
                } else {
45
                    $this->addError(new ResolveException(sprintf('Variable "%s" not exist for query "%s"', $argument->getName(), $queryType->getName())));
46
47
                    return false;
48
                }
49
            }
50
51 1
            if (!$argumentType->isValidValue($argumentType->parseValue($argument->getValue()->getValue()))) {
52
                $this->addError(new ResolveException(sprintf('Not valid type for argument "%s" in query "%s"', $argument->getName(), $queryType->getName())));
53
54
                return false;
55
            }
56
57 1
            if (array_key_exists($argument->getName(), $requiredArguments)) {
58 1
                unset($requiredArguments[$argument->getName()]);
59 1
            }
60 3
        }
61
62 3
        if (count($requiredArguments)) {
63
            $this->addError(new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName())));
64
65
            return false;
66
        }
67
68 3
        return true;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 3
    public function validateResolvedValue($value, $kind)
75
    {
76
        switch ($kind) {
77 3
            case TypeMap::KIND_OBJECT:
78 3
                return is_object($value) || is_null($value) || is_array($value);
79 1
            case TypeMap::KIND_LIST:
80 1
                return is_array($value);
81
        }
82
83
        return false;
84
    }
85
86
}