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

Passed
Push — 0.13 ( 161fda...ab14d1 )
by Jérémiah
15:28 queued 12:42
created

ArgumentsTransformer   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 34
eloc 75
c 2
b 1
f 0
dl 0
loc 191
ccs 70
cts 72
cp 0.9722
rs 9.68

6 Methods

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