GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — release-1.x ( a5e082...76423c )
by Boy
05:57 queued 02:54
created

JsonConvertibleParamConverter::apply()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
rs 8.439
cc 6
eloc 21
nc 7
nop 2
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupBundle\Request;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
22
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
23
use Surfnet\StepupBundle\Exception\BadJsonRequestException;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Validator\Validator\ValidatorInterface;
26
27
/**
28
 * ParamConverter that converts JSON objects with underscore notation mapped to snake-cased, public properties of
29
 * classes that implement JsonConvertible.
30
 *
31
 * @see JsonConvertible
32
 */
33
class JsonConvertibleParamConverter implements ParamConverterInterface
34
{
35
    /**
36
     * @var ValidatorInterface
37
     */
38
    private $validator;
39
40
    public function __construct(ValidatorInterface $validator)
41
    {
42
        $this->validator = $validator;
43
    }
44
45
    public function apply(Request $request, ParamConverter $configuration)
46
    {
47
        $name = $configuration->getName();
48
        $snakeCasedName = $this->camelCaseToSnakeCase($name);
49
        $class = $configuration->getClass();
50
51
        $json = $request->getContent();
52
        $object = json_decode($json, true);
53
54
        if (!isset($object[$snakeCasedName]) || !is_array($object[$snakeCasedName])) {
55
            throw new BadJsonRequestException([sprintf("Missing parameter '%s'", $name)]);
56
        }
57
58
        $object = $object[$snakeCasedName];
59
        $convertedObject = new $class;
60
61
        $errors = [];
62
63
        foreach ($object as $key => $value) {
64
            $properlyCasedKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
65
66
            if (!property_exists($convertedObject, $properlyCasedKey)) {
67
                $errors[] = sprintf("Unknown property '%s.%s'", $snakeCasedName, $key);
68
69
                continue;
70
            }
71
72
            $convertedObject->$properlyCasedKey = $value;
73
        }
74
75
        $violations = $this->validator->validate($convertedObject);
76
77
        if (count($errors) + count($violations) > 0) {
78
            throw BadJsonRequestException::createForViolationsAndErrors($violations, $name, $errors);
79
        }
80
81
        $request->attributes->set($name, $convertedObject);
82
    }
83
84
    public function supports(ParamConverter $configuration)
85
    {
86
        $class = $configuration->getClass();
87
88
        if (!is_string($class)) {
89
            return null;
90
        }
91
92
        return is_subclass_of($class, 'Surfnet\StepupBundle\Request\JsonConvertible');
93
    }
94
95
    /**
96
     * @param string $camelCase
97
     * @return string
98
     * @see \Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter
99
     */
100
    private function camelCaseToSnakeCase($camelCase)
101
    {
102
        $snakeCase = '';
103
104
        $len = strlen($camelCase);
105
        for ($i = 0; $i < $len; $i++) {
106
            if (ctype_upper($camelCase[$i])) {
107
                $snakeCase .= '_'.strtolower($camelCase[$i]);
108
            } else {
109
                $snakeCase .= strtolower($camelCase[$i]);
110
            }
111
        }
112
113
        return $snakeCase;
114
    }
115
}
116