Completed
Push — master ( d01340...7e7fa7 )
by Portey
07:36
created

ResolveValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 78.79%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 14
c 6
b 1
f 1
lcom 0
cbo 5
dl 0
loc 70
ccs 26
cts 33
cp 0.7879
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateArguments() 0 44 8
B validateResolvedValue() 0 12 6
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 12
        $requiredArguments = array_filter($queryType->getConfig()->getArguments(), function (Field $argument) {
29 5
            return $argument->getConfig()->get('required');
30 12
        });
31
32 12
        foreach ($query->getArguments() as $argument) {
33 4
            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 4
            $argumentType = $queryType->getConfig()->getArgument($argument->getName())->getType();
41 4
            if ($argument->getValue() instanceof Variable) {
42 1
                if ($request->hasVariable($argument->getValue()->getName())) {
43 1
                    $argument->getValue()->setValue($request->getVariable($argument->getValue()->getName()));
44 1
                } else {
45
                    $this->addError(new ResolveException(sprintf('Variable "%s" not exist for query "%s"', $argument->getName(), $queryType->getName())));
46
47
                    return false;
48
                }
49 1
            }
50
51 4
            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 4
            if (array_key_exists($argument->getName(), $requiredArguments)) {
58 2
                unset($requiredArguments[$argument->getName()]);
59 2
            }
60 12
        }
61
62 12
        if (count($requiredArguments)) {
63 1
            $this->addError(new ResolveException(sprintf('Require "%s" arguments to query "%s"', implode(', ', array_keys($requiredArguments)), $query->getName())));
64
65 1
            return false;
66
        }
67
68 11
        return true;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 11
    public function validateResolvedValue($value, $kind)
75
    {
76
        switch ($kind) {
77 11
            case TypeMap::KIND_OBJECT:
78 11
            case TypeMap::KIND_INTERFACE:
79 11
                return is_object($value) || is_null($value) || is_array($value);
80 5
            case TypeMap::KIND_LIST:
81 5
                return is_array($value);
82
        }
83
84
        return false;
85
    }
86
87
}