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
Pull Request — master (#325)
by Jérémiah
13:59
created

UploadParserTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.37%

Importance

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

5 Methods

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