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', array('groups' => array('group1')) |
37
|
|
|
); |
38
|
|
|
$response = new Response($json, Response::HTTP_OK); |
39
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
40
|
|
|
|
41
|
|
|
return $response; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Survey $survey |
46
|
|
|
* @Route("/survey/{id}", name="api_survey_byid") |
47
|
|
|
* @Method("GET") |
48
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
49
|
|
|
*/ |
50
|
|
|
public function apiSurveyAction(Survey $survey) |
51
|
|
|
{ |
52
|
|
|
$user = $this->getUser(); |
53
|
|
|
if (!$user) { |
54
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
55
|
|
|
} |
56
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
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', array('groups' => array('group1', 'group2')) |
66
|
|
|
); |
67
|
|
|
} else { |
68
|
|
|
$json = $serializer->serialize( |
69
|
|
|
$survey, |
70
|
|
|
'json', array('groups' => array('group1', 'group3')) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$response = new Response($json, Response::HTTP_OK); |
75
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
76
|
|
|
|
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
82
|
|
|
* @Route("/survey/update/{id}", name="api_survey_update") |
83
|
|
|
* @Method("POST") |
84
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
85
|
|
|
*/ |
86
|
|
|
public function apiSurveyUpdateAction(Request $request, Survey $survey) |
87
|
|
|
{ |
88
|
|
|
$user = $this->getUser(); |
89
|
|
|
if (!$user) { |
90
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
91
|
|
|
} |
92
|
|
|
if (!$survey || $survey->getStatus() == 'submited') { |
93
|
|
|
return $this->json(['message' => 'No survey'], 404); |
94
|
|
|
} |
95
|
|
|
foreach ($survey->getType()->getQuestions() as $question) { |
96
|
|
|
$quest_key[] = $question->getId(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
$data = json_decode($request->getContent(), true); |
99
|
|
|
$em = $this->getDoctrine()->getManager(); |
100
|
|
|
foreach ($quest_key as $key) { |
|
|
|
|
101
|
|
|
$answer = $data[$key]; |
102
|
|
|
if ($answer == null) { |
103
|
|
|
return $this->json(['message' => 'Wrong question id'], 400); |
104
|
|
|
} |
105
|
|
|
$new_answer = new SurveyAnswer(); |
106
|
|
|
$new_answer->setSurvey($survey); |
107
|
|
|
$new_answer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key)); |
|
|
|
|
108
|
|
|
$new_answer->setContent($answer); |
109
|
|
|
$em->persist($new_answer); |
110
|
|
|
} |
111
|
|
|
$survey->setStatus('submited'); |
112
|
|
|
$em->flush(); |
113
|
|
|
|
114
|
|
|
return $this->json(['message' => 'Survey updated'], 200); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.