Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — 0.13 (#745)
by Timur
12:56
created

ArgumentsTransformer   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Test Coverage

Coverage 97.1%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 32
eloc 72
c 2
b 1
f 0
dl 0
loc 186
ccs 67
cts 69
cp 0.971
rs 9.84

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A getTypeClassInstance() 0 5 3
A __construct() 0 5 1
A getArguments() 0 19 4
B populateObject() 0 53 11
C getInstanceAndValidate() 0 28 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Transformer;
6
7
use GraphQL\Type\Definition\EnumType;
8
use GraphQL\Type\Definition\InputObjectType;
9
use GraphQL\Type\Definition\ListOfType;
10
use GraphQL\Type\Definition\NonNull;
11
use GraphQL\Type\Definition\ResolveInfo;
12
use GraphQL\Type\Definition\Type;
13
use Overblog\GraphQLBundle\Error\InvalidArgumentError;
14
use Overblog\GraphQLBundle\Error\InvalidArgumentsError;
15
use Symfony\Component\PropertyAccess\PropertyAccess;
16
use Symfony\Component\PropertyAccess\PropertyAccessor;
17
use Symfony\Component\Validator\ConstraintViolationList;
18
use Symfony\Component\Validator\Validator\ValidatorInterface;
19
20
class ArgumentsTransformer
21
{
22
    /**
23
     * @var ValidatorInterface
24
     */
25
    protected $validator;
26
27
    /**
28
     * @var array
29
     */
30
    protected $classesMap;
31
32
    /**
33
     * @var PropertyAccessor
34
     */
35
    protected $accessor;
36
37 4
    public function __construct(ValidatorInterface $validator = null, $classesMap = [])
38
    {
39 4
        $this->validator = $validator;
40 4
        $this->accessor = PropertyAccess::createPropertyAccessor();
41 4
        $this->classesMap = $classesMap;
42 4
    }
43
44
    /**
45
     * Get the PHP class for a given type.
46
     *
47
     * @param string $type
48
     *
49
     * @return object|false
50
     */
51 4
    private function getTypeClassInstance(string $type)
52
    {
53 4
        $classname = isset($this->classesMap[$type]) ? $this->classesMap[$type]['class'] : false;
54
55 4
        return $classname ? new $classname() : false;
56
    }
57
58
    /**
59
     * Extract given type from Resolve Info.
60
     *
61
     * @param string      $type
62
     * @param ResolveInfo $info
63
     *
64
     * @return Type
65
     */
66 4
    private function getType(string $type, ResolveInfo $info): Type
67
    {
68 4
        return $info->schema->getType($type);
69
    }
70
71
    /**
72
     * Populate an object based on type with given data.
73
     *
74
     * @param Type        $type
75
     * @param mixed       $data
76
     * @param bool        $multiple
77
     * @param ResolveInfo $info
78
     *
79
     * @return mixed
80
     */
81 4
    private function populateObject(Type $type, $data, bool $multiple, ResolveInfo $info)
82
    {
83 4
        if (null === $data) {
84 4
            return $data;
85
        }
86
87 4
        if ($type instanceof NonNull) {
88
            $type = $type->getWrappedType();
89
        }
90
91 4
        if ($multiple) {
92
            return \array_map(function ($data) use ($type, $info) {
93 4
                return $this->populateObject($type, $data, false, $info);
94 4
            }, $data);
95
        }
96
97 4
        if ($type instanceof EnumType) {
98 4
            $instance = $this->getTypeClassInstance($type->name);
99 4
            if ($instance) {
100 2
                $this->accessor->setValue($instance, 'value', $data);
101
102 2
                return $instance;
103
            } else {
104 3
                return $data;
105
            }
106 4
        } elseif ($type instanceof InputObjectType) {
107 4
            $instance = $this->getTypeClassInstance($type->name);
108 4
            if (!$instance) {
109
                return $data;
110
            }
111
112 4
            $fields = $type->getFields();
113
114 4
            foreach ($fields as $name => $field) {
115 4
                $fieldData = $this->accessor->getValue($data, \sprintf('[%s]', $name));
116 4
                $fieldType = $field->getType();
117
118 4
                if ($fieldType instanceof NonNull) {
119 4
                    $fieldType = $fieldType->getWrappedType();
120
                }
121
122 4
                if ($fieldType instanceof ListOfType) {
123 4
                    $fieldValue = $this->populateObject($fieldType->getWrappedType(), $fieldData, true, $info);
124
                } else {
125 4
                    $fieldValue = $this->populateObject($fieldType, $fieldData, false, $info);
126
                }
127
128 4
                $this->accessor->setValue($instance, $name, $fieldValue);
129
            }
130
131 4
            return $instance;
132
        } else {
133 4
            return $data;
134
        }
135
    }
136
137
    /**
138
     * Given a GraphQL type and an array of data, populate corresponding object recursively
139
     * using annoted classes.
140
     *
141
     * @param string      $argType
142
     * @param mixed       $data
143
     * @param ResolveInfo $info
144
     *
145
     * @return mixed
146
     */
147 4
    public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info, string $argName)
148
    {
149 4
        $isRequired = '!' === $argType[\strlen($argType) - 1];
150 4
        $isMultiple = '[' === $argType[0];
151 4
        $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
152 4
        $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType));
153
154 4
        $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
155 4
        $errors = new ConstraintViolationList();
156 4
        if ($this->validator) {
157 4
            if (\is_object($result)) {
158 3
                $errors = $this->validator->validate($result);
159
            }
160 4
            if (\is_array($result) && $isMultiple) {
161 1
                foreach ($result as $element) {
162 1
                    if (\is_object($element)) {
163 1
                        $errors->addAll(
164 1
                            $this->validator->validate($element)
165
                        );
166
                    }
167
                }
168
            }
169
        }
170
171 4
        if (\count($errors) > 0) {
172 2
            throw new InvalidArgumentError($argName, $errors);
173
        } else {
174 2
            return $result;
175
        }
176
    }
177
178
    /**
179
     * Transform a list of arguments into their corresponding php class and validate them.
180
     *
181
     * @param array       $mapping
182
     * @param mixed       $data
183
     * @param ResolveInfo $info
184
     *
185
     * @return array
186
     */
187 4
    public function getArguments(array $mapping, $data, ResolveInfo $info)
188
    {
189 4
        $args = [];
190 4
        $exceptions = [];
191
192 4
        foreach ($mapping as $name => $type) {
193
            try {
194 4
                $value = $this->getInstanceAndValidate($type, $data[$name], $info, $name);
195 2
                $args[] = $value;
196 2
            } catch (InvalidArgumentError $exception) {
197 2
                $exceptions[] = $exception;
198
            }
199
        }
200
201 4
        if (!empty($exceptions)) {
202 2
            throw new InvalidArgumentsError($exceptions);
203
        }
204
205 2
        return $args;
206
    }
207
}
208