Completed
Push — dev ( a70858...5b49da )
by nonanerz
06:08 queued 06:04
created

SurveyController::editAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 38
Code Lines 26

Duplication

Lines 8
Ratio 21.05 %

Code Coverage

Tests 18
CRAP Score 6.7902

Importance

Changes 0
Metric Value
dl 8
loc 38
ccs 18
cts 25
cp 0.72
rs 8.439
c 0
b 0
f 0
cc 6
eloc 26
nc 6
nop 2
crap 6.7902
1
<?php
2
3
namespace AppBundle\Controller\Api;
4
5
use AppBundle\Entity\Survey\Survey;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use AppBundle\Exception\JsonHttpException;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Validator\ConstraintViolation;
13
14
class SurveyController extends Controller
15
{
16
    /**
17
     * @Route("/surveys", name="list_surveys")
18
     * @Method("GET")
19
     *
20
     * @return JsonResponse
21
     */
22 2
    public function listAction()
23
    {
24 2
        $surveys = $this->getDoctrine()
25 2
            ->getRepository(Survey::class)
26 2
            ->findSurveyByUser($this->getUser());
27 2
        $json = $this->get('serializer')->normalize($surveys, null, array('groups' => array('list')));
28
29 2
        return $this->json(['surveys' => $json], 200);
30
    }
31
32
    /**
33
     * @param int $id
34
     * @Route("/surveys/{id}",  requirements={"id": "\d+"}, name="show_survey")
35
     * @Method("GET")
36
     *
37
     * @return JsonResponse
38
     */
39
    public function showAction($id)
40
    {
41
        $user = $this->getUser();
42
        $em = $this->getDoctrine()->getManager();
43
        $survey = $em->getRepository(Survey::class)->find($id);
44
        if ($user !== $survey->getUser()) {
45
            throw new JsonHttpException(403, "Current user doesn't have accesses to this resource");
46
        }
47
48
        return $this->json(['survey' => $survey], 200);
49
    }
50
51
    /**
52
     * @param Request $request, int $id
0 ignored issues
show
Documentation introduced by
There is no parameter named $request,. Did you maybe mean $request?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
53
     * @Route("/surveys/{id}", requirements={"id": "\d+"}, name="edit_survey")
54
     * @Method("PUT")
55
     *
56
     * @return JsonResponse
57
     */
58 1
    public function editAction(Request $request, $id)
59
    {
60 1
        $user = $this->getUser();
61 1
        $em = $this->getDoctrine()->getManager();
62 1
        $data = $request->getContent();
63 1
        if (!$data) {
64
            throw new JsonHttpException(400, 'Bad Request.');
65
        }
66 1
        $survey = $em->getRepository(Survey::class)->find($id);
67
68 1
        if ($user !== $survey->getUser()) {
69
            throw new JsonHttpException(403, "Current user can't change this resource");
70
        }
71 1
        if ('current' !== $survey->getStatus()) {
72
            throw new JsonHttpException(404, 'Survey was already submited');
73
        }
74 1
        $serializer = $this->get('serializer');
75
76 1
        $survey = $serializer->deserialize(
77
            $data,
78 1
            Survey::class,
79 1
            'json',
80 1
            array('object_to_populate' => $survey)
81
        );
82 1
        $errors = $this->get('validator')->validate($survey);
83 1 View Code Duplication
        if ($errors->count()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            $outErrors = [];
85
            /** @var ConstraintViolation $error */
86
            foreach ($errors as $error) {
87
                $outErrors[$error->getPropertyPath()] = $error->getMessage();
88
            }
89
            throw new JsonHttpException(400, 'Bad Request', $outErrors);
90
        }
91 1
        $em->persist($survey);
92 1
        $em->flush();
93
94 1
        return $this->json(['message' => 'Survey is updated'], 200);
95
    }
96
}
97