Completed
Push — master ( 982da4...e066f4 )
by Alexandr
02:57
created

AbstractInputObjectType::isValidValue()   D

Complexity

Conditions 10
Paths 18

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10.0658

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 21
cts 23
cp 0.913
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 18
nc 18
nop 1
crap 10.0658

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
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/2/15 9:00 PM
7
*/
8
9
namespace Youshido\GraphQL\Type\InputObject;
10
11
12
use Youshido\GraphQL\Config\Object\InputObjectTypeConfig;
13
use Youshido\GraphQL\Field\FieldInterface;
14
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject;
15
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
16
use Youshido\GraphQL\Type\AbstractType;
17
use Youshido\GraphQL\Type\Traits\AutoNameTrait;
18
use Youshido\GraphQL\Type\Traits\FieldsAwareObjectTrait;
19
use Youshido\GraphQL\Type\TypeMap;
20
21
abstract class AbstractInputObjectType extends AbstractType
22
{
23
24
    use AutoNameTrait, FieldsAwareObjectTrait;
25
26 1
    public function __construct($config = [])
27
    {
28 1
        if (empty($config)) {
29
            $config = [
30 1
                'name' => $this->getName()
31 1
            ];
32 1
        }
33 1
        $this->config = new InputObjectTypeConfig($config, $this);
34 1
        $this->build($this->config);
35 1
    }
36
37
    /**
38
     * @param InputObjectTypeConfig $config
39
     */
40
    abstract public function build($config);
41
42 7
    public function isValidValue($value)
43
    {
44 7
        if ($value instanceof InputObject) {
45
            $value = $value->getValue();
46
        }
47
48 7
        if (empty($value)) {
49 1
            return true;
50
        }
51
52 6
        if (!is_array($value)) {
53 1
            return false;
54
        }
55
56 6
        $typeConfig     = $this->getConfig();
57 6
        $requiredFields = array_filter($typeConfig->getFields(), function (FieldInterface $field) {
58 6
            return $field->getType()->getKind() == TypeMap::KIND_NON_NULL;
59 6
        });
60
61 6
        foreach ($value as $valueKey => $valueItem) {
62 6
            if (!$typeConfig->hasField($valueKey) || !$typeConfig->getField($valueKey)->getType()->isValidValue($valueItem)) {
63 3
                return false;
64
            }
65
66 5
            if (array_key_exists($valueKey, $requiredFields)) {
67 5
                unset($requiredFields[$valueKey]);
68 5
            }
69 5
        }
70 5
        if (count($requiredFields)) {
71 1
            $this->lastError = sprintf('%s %s required on %s', implode(', ', array_keys($requiredFields)), count($requiredFields) > 1 ? 'are' : 'is', $typeConfig->getName());
72 1
        }
73
74 5
        return !(count($requiredFields) > 0);
75
    }
76
77 8
    public function getKind()
78
    {
79 8
        return TypeMap::KIND_INPUT_OBJECT;
80
    }
81
82 2
    public function isInputType()
83
    {
84 2
        return true;
85
    }
86
87 6
    public function parseValue($value)
88
    {
89 6
        if($value instanceof InputObject) {
90
            $value = $value->getValue();
91
        }
92
93 6
        $typeConfig = $this->getConfig();
94 6
        foreach ($value as $valueKey => $item) {
95 5
            if ($item instanceof Variable) {
96
                $item = $item->getValue();
97
            }
98
99 5
            if (!($inputField = $typeConfig->getField($valueKey))) {
100
                throw new \Exception(sprintf('Invalid field "%s" on %s', $valueKey, $typeConfig->getName()));
101
            }
102 5
            $value[$valueKey] = $inputField->getType()->parseValue($item);
103 6
        }
104
105 6
        return $value;
106
    }
107
108
}
109