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
|
|
|
|
14
|
|
|
class SurveyController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @Route("/surveys", name="api_surveys") |
18
|
|
|
* @Method("GET") |
19
|
|
|
*/ |
20
|
|
|
public function apiSurveysAction() |
21
|
|
|
{ |
22
|
|
|
$user = $this->getUser(); |
23
|
|
|
if (!$user) { |
24
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
25
|
|
|
} |
26
|
|
|
$em = $this->getDoctrine()->getManager(); |
27
|
|
|
$surveys = $em->getRepository(Survey::class)->findSurveyByUser($user); |
28
|
|
|
if (!$surveys) { |
29
|
|
|
return $this->json(['message' => 'No surveys'], 404); |
30
|
|
|
} |
31
|
|
|
$serializer = $this->get('serializer'); |
32
|
|
|
|
33
|
|
|
$json = $serializer->normalize( |
34
|
|
|
$surveys, |
35
|
|
|
null, |
36
|
|
|
array('groups' => array('group1')) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
return $this->json($json); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Survey $survey |
44
|
|
|
* @Route("/survey/{id}", name="api_survey_byid") |
45
|
|
|
* @Method("GET") |
46
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
47
|
|
|
*/ |
48
|
|
|
public function apiSurveyAction(Survey $survey) |
49
|
|
|
{ |
50
|
|
|
$user = $this->getUser(); |
51
|
|
|
if (!$user) { |
52
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!$survey) { |
56
|
|
|
return $this->json(['message' => 'No surveys'], 404); |
57
|
|
|
} |
58
|
|
|
$serializer = $this->get('serializer'); |
59
|
|
|
if ($survey->getStatus() == 'submited') { |
60
|
|
|
$json = $serializer->normalize( |
61
|
|
|
$survey, |
62
|
|
|
null, |
63
|
|
|
array('groups' => array('group1', 'group2')) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
if ($survey->getStatus() == 'current') { |
67
|
|
|
$json = $serializer->normalize( |
68
|
|
|
$survey, |
69
|
|
|
null, |
70
|
|
|
array('groups' => array('group1', 'group3')) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->json($json); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
79
|
|
|
* @Route("/survey/update/{id}", name="api_survey_update") |
80
|
|
|
* @Method("POST") |
81
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
82
|
|
|
*/ |
83
|
|
|
public function apiSurveyUpdateAction(Request $request, Survey $survey) |
84
|
|
|
{ |
85
|
|
|
$user = $this->getUser(); |
86
|
|
|
if (!$user) { |
87
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
88
|
|
|
} |
89
|
|
|
if (!$survey || $survey->getStatus() == 'submited') { |
90
|
|
|
return $this->json(['message' => 'No survey'], 404); |
91
|
|
|
} |
92
|
|
|
$questKey = array(); |
93
|
|
|
foreach ($survey->getType()->getQuestions() as $question) { |
94
|
|
|
$questKey[] = $question->getId(); |
95
|
|
|
} |
96
|
|
|
$data = json_decode($request->getContent(), true); |
97
|
|
|
$em = $this->getDoctrine()->getManager(); |
98
|
|
|
foreach ($questKey as $key) { |
99
|
|
|
$answer = $data[$key]; |
100
|
|
|
if ($answer == null) { |
101
|
|
|
return $this->json(['message' => 'Wrong question id'], 400); |
102
|
|
|
} |
103
|
|
|
$newAnswer = new SurveyAnswer(); |
104
|
|
|
$newAnswer->setSurvey($survey); |
105
|
|
|
$newAnswer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key)); |
|
|
|
|
106
|
|
|
$newAnswer->setContent($answer); |
107
|
|
|
$em->persist($newAnswer); |
108
|
|
|
} |
109
|
|
|
$survey->setStatus('submited'); |
110
|
|
|
$em->flush(); |
111
|
|
|
|
112
|
|
|
return $this->json(['message' => 'Survey updated'], 200); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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: