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
|
|
|
} |
69
|
|
|
if ($survey->getStatus() == 'current') { |
70
|
|
|
$json = $serializer->serialize( |
71
|
|
|
$survey, |
72
|
|
|
'json', |
73
|
|
|
array('groups' => array('group1', 'group3')) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$response = new Response($json, Response::HTTP_OK); |
|
|
|
|
78
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
79
|
|
|
|
80
|
|
|
return $response; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
85
|
|
|
* @Route("/survey/update/{id}", name="api_survey_update") |
86
|
|
|
* @Method("POST") |
87
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
88
|
|
|
*/ |
89
|
|
|
public function apiSurveyUpdateAction(Request $request, Survey $survey) |
90
|
|
|
{ |
91
|
|
|
$user = $this->getUser(); |
92
|
|
|
if (!$user) { |
93
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
94
|
|
|
} |
95
|
|
|
if (!$survey || $survey->getStatus() == 'submited') { |
96
|
|
|
return $this->json(['message' => 'No survey'], 404); |
97
|
|
|
} |
98
|
|
|
$questKey = array(); |
99
|
|
|
foreach ($survey->getType()->getQuestions() as $question) { |
100
|
|
|
$questKey[] = $question->getId(); |
101
|
|
|
} |
102
|
|
|
$data = json_decode($request->getContent(), true); |
103
|
|
|
$em = $this->getDoctrine()->getManager(); |
104
|
|
|
foreach ($questKey as $key) { |
105
|
|
|
$answer = $data[$key]; |
106
|
|
|
if ($answer == null) { |
107
|
|
|
return $this->json(['message' => 'Wrong question id'], 400); |
108
|
|
|
} |
109
|
|
|
$newAnswer = new SurveyAnswer(); |
110
|
|
|
$newAnswer->setSurvey($survey); |
111
|
|
|
$newAnswer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key)); |
|
|
|
|
112
|
|
|
$newAnswer->setContent($answer); |
113
|
|
|
$em->persist($newAnswer); |
114
|
|
|
} |
115
|
|
|
$survey->setStatus('submited'); |
116
|
|
|
$em->flush(); |
117
|
|
|
|
118
|
|
|
return $this->json(['message' => 'Survey updated'], 200); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: