SubmissionsController::post()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 1
dl 0
loc 34
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Scenario;
4
5
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\Bridge\...ion\ValidationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use App\Entity\Submission;
7
use App\Service\ScenarioService;
8
use App\Service\SubmissionService;
9
use InvalidArgumentException;
10
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...BadRequestHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...n\NotFoundHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Validator\ConstraintViolationList;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Valida...ConstraintViolationList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use function GuzzleHttp\json_decode;
0 ignored issues
show
introduced by
The function GuzzleHttp\json_decode was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
18
use Symfony\Component\Routing\Annotation\Route;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Routing\Annotation\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * Class SubmissionsController
22
 */
23
final class SubmissionsController
24
{
25
    /**
26
     * @var \Symfony\Component\HttpFoundation\RequestStack
27
     */
28
    private $requestStack;
29
30
    /**
31
     * @var \App\Service\ScenarioService
32
     */
33
    private $scenarioService;
34
35
    /**
36
     * @var \App\Service\SubmissionService
37
     */
38
    private $submissionService;
39
40
    /**
41
     * Constructor
42
     *
43
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
44
     * @param \App\Service\ScenarioService $scenarioService
45
     * @param \App\Service\SubmissionService $submissionService
46
     */
47
    public function __construct(RequestStack $requestStack, ScenarioService $scenarioService, SubmissionService $submissionService)
48
    {
49
        $this->requestStack = $requestStack;
50
        $this->scenarioService = $scenarioService;
51
        $this->submissionService = $submissionService;
52
    }
53
54
    /**
55
     * Form
56
     *
57
     * @Route(path="/scenarios/{uuid}/submissions", methods={"POST"})
58
     * @param string $uuid
59
     * @return \Symfony\Component\HttpFoundation\JsonResponse
60
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
61
     */
62
    public function post($uuid)
63
    {
64
        $scenario = $this->scenarioService->getRepository()->findOneBy(['uuid' => $uuid]);
65
66
        if (!$scenario) {
67
            throw new NotFoundHttpException('Scenario not found.');
68
        }
69
70
        $request = $this->requestStack->getCurrentRequest();
71
72
        try {
73
            $content = json_decode($request->getContent());
74
        } catch (InvalidArgumentException $exception) {
75
            throw new BadRequestHttpException('Request content is not valid json.');
76
        }
77
78
        $submission = $this->submissionService->createInstance();
79
        $submission
80
            ->setScenario($scenario)
81
            ->setData((array) $content->data)
82
            ->setState(Submission::STATE_SUBMITTED);
83
        $violations = [];
84
85
        if (!$this->submissionService->isValid($submission, $violations)) {
86
            $list = new ConstraintViolationList($violations);
87
            throw new ValidationException($list, 'An error occurred');
88
        }
89
90
        $manager = $this->submissionService->getManager();
91
        $manager->persist($submission);
92
        $manager->flush();
93
        $response = new JsonResponse($submission, Response::HTTP_CREATED);
94
95
        return $response;
96
    }
97
}
98