Completed
Pull Request — dev (#24)
by
unknown
04:00
created

SurveyController::apiSurveyAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 4
nop 1
1
<?php
2
3
namespace AppBundle\Controller\Api;
4
5
use AppBundle\Entity\SurveyAnswer;
6
use AppBundle\Entity\Survey;
7
use AppBundle\Entity\SurveyQuestion;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class SurveyController extends Controller
16
{
17
    /**
18
     * @Route("/surveys", name="api_surveys")
19
     * @Method("GET")
20
     */
21
    public function apiSurveysAction()
22
    {
23
        $user = $this->getUser();
24
        if (!$user) {
25
            return $this->json(['message' => 'User is not authorized'], 401);
26
        }
27
        $em = $this->getDoctrine()->getManager();
28
        $surveys = $em->getRepository(Survey::class)->findSurveyByUser($user);
29
        if (!$surveys) {
30
            return $this->json(['message' => 'No surveys'], 404);
31
        }
32
        $serializer = $this->get('serializer');
33
34
        $json = $serializer->serialize(
35
            $surveys,
36
            'json',
37
            array('groups' => array('group1'))
38
        );
39
        $response = new Response($json, Response::HTTP_OK);
40
        $response->headers->set('Content-Type', 'application/json');
41
42
        return $response;
43
    }
44
45
    /**
46
     * @param Survey $survey
47
     * @Route("/survey/{id}", name="api_survey_byid")
48
     * @Method("GET")
49
     * @ParamConverter("survey", class="AppBundle:Survey")
50
     */
51
    public function apiSurveyAction(Survey $survey)
52
    {
53
        $user = $this->getUser();
54
        if (!$user) {
55
            return $this->json(['message' => 'User is not authorized'], 401);
56
        }
57
58
        if (!$survey) {
59
            return $this->json(['message' => 'No surveys'], 404);
60
        }
61
        $serializer = $this->get('serializer');
62
        if ($survey->getStatus() == 'submited') {
63
            $json = $serializer->serialize(
64
                $survey,
65
                'json',
66
                array('groups' => array('group1', 'group2'))
67
            );
68
        } else {
69
            $json = $serializer->serialize(
70
                $survey,
71
                'json',
72
                array('groups' => array('group1', 'group3'))
73
            );
74
        }
75
76
        $response = new Response($json, Response::HTTP_OK);
77
        $response->headers->set('Content-Type', 'application/json');
78
79
        return $response;
80
    }
81
82
    /**
83
     * @param Request $request, Survey $survey
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...
84
     * @Route("/survey/update/{id}", name="api_survey_update")
85
     * @Method("POST")
86
     * @ParamConverter("survey", class="AppBundle:Survey")
87
     */
88
    public function apiSurveyUpdateAction(Request $request, Survey $survey)
89
    {
90
        $user = $this->getUser();
91
        if (!$user) {
92
            return $this->json(['message' => 'User is not authorized'], 401);
93
        }
94
        if (!$survey || $survey->getStatus() == 'submited') {
95
            return $this->json(['message' => 'No survey'], 404);
96
        }
97
        $questKey = array();
98
        foreach ($survey->getType()->getQuestions() as $question) {
99
            $questKey[] = $question->getId();
100
        }
101
        $data = json_decode($request->getContent(), true);
102
        $em = $this->getDoctrine()->getManager();
103
        foreach ($questKey as $key) {
104
            $answer = $data[$key];
105
            if ($answer == null) {
106
                return $this->json(['message' => 'Wrong question id'], 400);
107
            }
108
            $newAnswer = new SurveyAnswer();
109
            $newAnswer->setSurvey($survey);
110
            $newAnswer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key));
0 ignored issues
show
Documentation introduced by
$em->getRepository(\AppB...ion::class)->find($key) is of type object|null, but the function expects a object<AppBundle\Entity\SurveyQuestion>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
            $newAnswer->setContent($answer);
112
            $em->persist($newAnswer);
113
        }
114
        $survey->setStatus('submited');
115
        $em->flush();
116
117
        return $this->json(['message' => 'Survey updated'], 200);
118
    }
119
}
120