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

UploadParserTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 69
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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