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 — master (#516)
by Andrey
17:05 queued 08:25
created

ArgumentsTransformer   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Test Coverage

Coverage 98.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 31
eloc 69
c 1
b 0
f 0
dl 0
loc 181
ccs 65
cts 66
cp 0.9848
rs 9.92

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 48 10
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 3
    public function __construct(ValidatorInterface $validator = null, $classesMap = [])
38
    {
39 3
        $this->validator = $validator;
40 3
        $this->accessor = PropertyAccess::createPropertyAccessor();
41 3
        $this->classesMap = $classesMap;
42 3
    }
43
44
    /**
45
     * Get the PHP class for a given type.
46
     *
47
     * @param string $type
48
     *
49
     * @return object|false
50
     */
51 3
    private function getTypeClassInstance(string $type)
52
    {
53 3
        $classname = isset($this->classesMap[$type]) ? $this->classesMap[$type]['class'] : false;
54
55 3
        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 3
    private function getType(string $type, ResolveInfo $info): Type
67
    {
68 3
        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 3
    private function populateObject(Type $type, $data, bool $multiple, ResolveInfo $info)
82
    {
83 3
        if (null === $data) {
84 3
            return $data;
85
        }
86
87 3
        if ($type instanceof NonNull) {
88 1
            $type = $type->getWrappedType();
89
        }
90
91 3
        if ($multiple) {
92
            return \array_map(function ($data) use ($type, $info) {
93 3
                return $this->populateObject($type, $data, false, $info);
94 3
            }, $data);
95
        }
96
97 3
        if ($type instanceof EnumType) {
98 3
            $instance = $this->getTypeClassInstance($type->name);
99 3
            if ($instance) {
100 1
                $this->accessor->setValue($instance, 'value', $data);
101
102 1
                return $instance;
103
            } else {
104 3
                return $data;
105
            }
106 3
        } elseif ($type instanceof InputObjectType) {
107 3
            $instance = $this->getTypeClassInstance($type->name);
108 3
            if (!$instance) {
109
                return $data;
110
            }
111
112 3
            $fields = $type->getFields();
113
114 3
            foreach ($fields as $name => $field) {
115 3
                $fieldData = $this->accessor->getValue($data, \sprintf('[%s]', $name));
116
117 3
                if ($field->getType() instanceof ListOfType) {
118 3
                    $fieldValue = $this->populateObject($field->getType()->getWrappedType(), $fieldData, true, $info);
119
                } else {
120 3
                    $fieldValue = $this->populateObject($field->getType(), $fieldData, false, $info);
121
                }
122
123 3
                $this->accessor->setValue($instance, $name, $fieldValue);
124
            }
125
126 3
            return $instance;
127
        } else {
128 3
            return $data;
129
        }
130
    }
131
132
    /**
133
     * Given a GraphQL type and an array of data, populate corresponding object recursively
134
     * using annoted classes.
135
     *
136
     * @param string      $argType
137
     * @param mixed       $data
138
     * @param ResolveInfo $info
139
     *
140
     * @return mixed
141
     */
142 3
    public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info, string $argName)
143
    {
144 3
        $isRequired = '!' === $argType[\strlen($argType) - 1];
145 3
        $isMultiple = '[' === $argType[0];
146 3
        $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0);
147 3
        $type = \substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : \strlen($argType));
148
149 3
        $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info);
150 3
        $errors = new ConstraintViolationList();
151 3
        if ($this->validator) {
152 3
            if (\is_object($result)) {
153 2
                $errors = $this->validator->validate($result);
154
            }
155 3
            if (\is_array($result) && $isMultiple) {
156 1
                foreach ($result as $element) {
157 1
                    if (\is_object($element)) {
158 1
                        $errors->addAll(
159 1
                            $this->validator->validate($element)
160
                        );
161
                    }
162
                }
163
            }
164
        }
165
166 3
        if (\count($errors) > 0) {
167 2
            throw new InvalidArgumentError($argName, $errors);
168
        } else {
169 1
            return $result;
170
        }
171
    }
172
173
    /**
174
     * Transform a list of arguments into their corresponding php class and validate them.
175
     *
176
     * @param array       $mapping
177
     * @param mixed       $data
178
     * @param ResolveInfo $info
179
     *
180
     * @return array
181
     */
182 3
    public function getArguments(array $mapping, $data, ResolveInfo $info)
183
    {
184 3
        $args = [];
185 3
        $exceptions = [];
186
187 3
        foreach ($mapping as $name => $type) {
188
            try {
189 3
                $value = $this->getInstanceAndValidate($type, $data[$name], $info, $name);
190 1
                $args[] = $value;
191 2
            } catch (InvalidArgumentError $exception) {
192 2
                $exceptions[] = $exception;
193
            }
194
        }
195
196 3
        if (!empty($exceptions)) {
197 2
            throw new InvalidArgumentsError($exceptions);
198
        }
199
200 1
        return $args;
201
    }
202
}
203