checkVersionConstraintAction()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 45
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 24
nc 4
nop 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @copyright  2015 Christian Schiffler <[email protected]>
16
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
17
 * @link       https://github.com/tenside/core-bundle
18
 * @filesource
19
 */
20
21
namespace Tenside\CoreBundle\Controller;
22
23
use Composer\Semver\VersionParser;
24
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Symfony\Component\HttpFoundation\JsonResponse;
27
use Symfony\Component\HttpFoundation\Request;
28
use Tenside\Core\Util\JsonArray;
29
use Tenside\CoreBundle\Annotation\ApiDescription;
30
31
/**
32
 * Validation of version constraints.
33
 */
34
class VersionConstraintController extends Controller
35
{
36
    /**
37
     * Try to validate the version constraint.
38
     *
39
     * @param Request $request The request.
40
     *
41
     * @return JsonResponse
42
     *
43
     * @throws \RuntimeException For invalid user classes.
44
     *
45
     * @ApiDoc(
46
     *   section="misc",
47
     *   statusCodes = {
48
     *     200 = "When everything worked out ok",
49
     *     400 = "When the request payload was invalid."
50
     *   },
51
     *   authentication = true,
52
     *   authenticationRoles = {
53
     *     "ROLE_NONE"
54
     *   }
55
     * )
56
     * @ApiDescription(
57
     *   request={
58
     *     "constraint" = {
59
     *       "description" = "The constraint to test.",
60
     *       "dataType" = "string",
61
     *       "required" = true
62
     *     }
63
     *   },
64
     *   response={
65
     *    "status" = {
66
     *      "dataType" = "choice",
67
     *      "description" = "OK or ERROR",
68
     *      "format" = "['OK', 'ERROR']",
69
     *    },
70
     *    "error" = {
71
     *      "dataType" = "string",
72
     *      "description" = "The error message (if any).",
73
     *    }
74
     *   }
75
     * )
76
     */
77
    public function checkVersionConstraintAction(Request $request)
78
    {
79
        try {
80
            $inputData = new JsonArray($request->getContent());
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, Tenside\Core\Util\JsonArray::__construct() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
81
        } catch (\Exception $exception) {
82
            return new JsonResponse(
83
                [
84
                    'status' => 'ERROR',
85
                    'error'  => 'invalid payload'
86
                ],
87
                JsonResponse::HTTP_BAD_REQUEST
88
            );
89
        }
90
91
        $versionParser = new VersionParser();
92
93
        if (!$inputData->has('constraint')) {
94
            return new JsonResponse(
95
                [
96
                    'status' => 'ERROR',
97
                    'error'  => 'invalid payload'
98
                ],
99
                JsonResponse::HTTP_BAD_REQUEST
100
            );
101
        }
102
103
        try {
104
            $versionParser->parseConstraints($inputData->get('constraint'));
105
        } catch (\Exception $exception) {
106
            return new JsonResponse(
107
                [
108
                    'status' => 'ERROR',
109
                    'error'  => $exception->getMessage()
110
                ],
111
                JsonResponse::HTTP_OK
112
            );
113
        }
114
115
        return new JsonResponse(
116
            [
117
                'status' => 'OK',
118
            ],
119
            JsonResponse::HTTP_OK
120
        );
121
    }
122
}
123