MultiPartFileUploadMiddleware::processRequest()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 21
cp 0
rs 8.0555
cc 9
nc 7
nop 2
crap 90
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Request;
12
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * Middleware to support "GraphQL multipart request specification"
17
 *
18
 * @see https://github.com/jaydenseric/graphql-multipart-request-spec
19
 */
20
class MultiPartFileUploadMiddleware implements RequestMiddlewareInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function processRequest(Request $request, ExecuteQuery $query): void
26
    {
27
        //TODO: add support for batching
28
        if (mb_stripos($request->headers->get('Content-Type'), 'multipart/form-data') !== false) {
29
            if (($operations = $request->get('operations')) && ($map = $request->get('map'))) {
30
                $result = json_decode($operations, true);
31
                $map = json_decode($map, true);
32
                foreach ($map as $fileKey => $locations) {
33
                    foreach ($locations as $location) {
34
                        $items = &$result;
35
                        foreach (explode('.', $location) as $key) {
36
                            if (!isset($items[$key]) || !is_array($items[$key])) {
37
                                $items[$key] = [];
38
                            }
39
                            $items = &$items[$key];
40
                        }
41
42
                        $items = $request->files->get($fileKey);
43
                    }
44
                }
45
46
                $query->setRequestString($result['query'] ?? null);
0 ignored issues
show
Bug introduced by
It seems like $result['query'] ?? null can also be of type null; however, parameter $requestString of Ynlo\GraphQLBundle\Reque...ery::setRequestString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                $query->setRequestString(/** @scrutinizer ignore-type */ $result['query'] ?? null);
Loading history...
47
                $query->setVariables($result['variables'] ?? []);
48
                $query->setOperationName($result['operationName'] ?? null);
49
            }
50
        }
51
    }
52
}
53