Failed Conditions
Push — master ( 392b56...c3d69c )
by Vladimir
03:59
created

InputObjectField::assertValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0884

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 19
ccs 11
cts 14
cp 0.7856
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0884
1
<?php
2
namespace GraphQL\Type\Definition;
3
use GraphQL\Error\Error;
4
use GraphQL\Error\InvariantViolation;
5
use GraphQL\Language\AST\InputValueDefinitionNode;
6
use GraphQL\Utils\Utils;
7
8
/**
9
 * Class InputObjectField
10
 * @package GraphQL\Type\Definition
11
 */
12
class InputObjectField
13
{
14
    /**
15
     * @var string
16
     */
17
    public $name;
18
19
    /**
20
     * @var mixed|null
21
     */
22
    public $defaultValue;
23
24
    /**
25
     * @var string|null
26
     */
27
    public $description;
28
29
    /**
30
     * @var callback|InputType
31
     */
32
    public $type;
33
34
    /**
35
     * @var InputValueDefinitionNode|null
36
     */
37
    public $astNode;
38
39
    /**
40
     * @var array
41
     */
42
    public $config;
43
44
    /**
45
     * Helps to differentiate when `defaultValue` is `null` and when it was not even set initially
46
     *
47
     * @var bool
48
     */
49
    private $defaultValueExists = false;
50
51
    /**
52
     * InputObjectField constructor.
53
     * @param array $opts
54
     */
55 430
    public function __construct(array $opts)
56
    {
57 430
        foreach ($opts as $k => $v) {
58
            switch ($k) {
59 430
                case 'defaultValue':
60 8
                    $this->defaultValue = $v;
61 8
                    $this->defaultValueExists = true;
62 8
                    break;
63 430
                case 'defaultValueExists':
64
                    break;
65
                default:
66 430
                    $this->{$k} = $v;
67
            }
68
        }
69 430
        $this->config = $opts;
70 430
    }
71
72
    /**
73
     * @return mixed
74
     */
75 428
    public function getType()
76
    {
77 428
        return $this->type;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83 13
    public function defaultValueExists()
84
    {
85 13
        return $this->defaultValueExists;
86
    }
87
88
    /**
89
     * @param Type $parentType
90
     * @throws InvariantViolation
91
     */
92 4
    public function assertValid(Type $parentType)
93
    {
94
        try {
95 4
            Utils::assertValidName($this->name);
96
        } catch (Error $e) {
97
            throw new InvariantViolation("{$parentType->name}.{$this->name}: {$e->getMessage()}");
98
        }
99 4
        $type = $this->type;
100 4
        if ($type instanceof WrappingType) {
101
            $type = $type->getWrappedType(true);
102
        }
103 4
        Utils::invariant(
104 4
            $type instanceof InputType,
105 4
            "{$parentType->name}.{$this->name} field type must be Input Type but got: " . Utils::printSafe($this->type)
106
        );
107 4
        Utils::invariant(
108 4
            empty($this->config['resolve']),
109 4
            "{$parentType->name}.{$this->name} field type has a resolve property, " .
110 4
            'but Input Types cannot define resolvers.'
111
        );
112 2
    }
113
}
114