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 (#288)
by Jérémiah
15:52
created

UploadParserTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 71
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleUploadedFiles() 0 9 2
B bindUploadedFiles() 0 23 5
B isUploadPayload() 0 13 5
A locationToPropertyAccessPath() 0 9 1
A normalized() 0 10 4
1
<?php
2
3
namespace Overblog\GraphQLBundle\Request;
4
5
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
6
use Symfony\Component\PropertyAccess\PropertyAccess;
7
8
trait UploadParserTrait
9
{
10 9
    private function handleUploadedFiles(array $parameters, array $files)
11
    {
12 9
        $payload = $this->normalized($parameters);
13 9
        if ($this->isUploadPayload($payload)) {
14 8
            return $this->bindUploadedFiles($payload['operations'], $payload['map'], $files);
15
        } else {
16 1
            return $parameters;
17
        }
18
    }
19
20 10
    private function bindUploadedFiles(array $operations, array $map, array $files)
21
    {
22 10
        $accessor = PropertyAccess::createPropertyAccessorBuilder()
23 10
            ->enableExceptionOnInvalidIndex()
24 10
            ->getPropertyAccessor();
25
26 10
        foreach ($map as $fileName => $locations) {
27 10
            foreach ($locations as $location) {
28 10
                $fileKey = sprintf('[%s]', $fileName);
29 10
                if (!$accessor->isReadable($files, $fileKey)) {
30 1
                    throw new BadRequestHttpException(sprintf('File %s is missing in the request.', json_encode($fileName)));
31
                }
32 9
                $file = $accessor->getValue($files, $fileKey);
33 9
                $locationKey = $this->locationToPropertyAccessPath($location);
34 9
                if (!$accessor->isReadable($operations, $locationKey)) {
35 1
                    throw new BadRequestHttpException(sprintf('Map entry %s could not be localized in operations.', json_encode($location)));
36
                }
37 8
                $accessor->setValue($operations, $locationKey, $file);
38
            }
39
        }
40
41 8
        return $operations;
42
    }
43
44 10
    private function isUploadPayload(array $payload)
45
    {
46 10
        if (isset($payload['operations']) && isset($payload['map']) && is_array($payload['operations']) && is_array($payload['map'])) {
47 9
            $payloadKeys = array_keys($payload);
48
            // the specs says that operations must be place before map
49 9
            $operationsPosition = array_search('operations', $payloadKeys);
50 9
            $mapPosition = array_search('map', $payloadKeys);
51
52 9
            return $operationsPosition < $mapPosition;
53
        } else {
54 2
            return false;
55
        }
56
    }
57
58 15
    private function locationToPropertyAccessPath($location)
59
    {
60 15
        return array_reduce(
61 15
            explode('.', $location),
62 15
            function ($carry, $item) {
63 15
                return sprintf('%s[%s]', $carry, $item);
64 15
            }
65
        );
66
    }
67
68 9
    private function normalized(array $parsedBody)
69
    {
70 9
        foreach (['operations', 'map'] as $key) {
71 9
            if (isset($parsedBody[$key]) && is_string($parsedBody[$key])) {
72 9
                $parsedBody[$key] = json_decode($parsedBody[$key], true);
73
            }
74
        }
75
76 9
        return $parsedBody;
77
    }
78
}
79