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
|
2 |
|
public function apiSurveysAction() |
21
|
|
|
{ |
22
|
2 |
|
$user = $this->getUser(); |
23
|
2 |
|
if (!$user) { |
24
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
25
|
|
|
} |
26
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
27
|
2 |
|
$surveys = $em->getRepository(Survey::class)->findSurveyByUser($user); |
28
|
2 |
|
if (!$surveys) { |
29
|
|
|
return $this->json(['message' => 'No surveys'], 404); |
30
|
|
|
} |
31
|
2 |
|
$serializer = $this->get('serializer'); |
32
|
|
|
|
33
|
2 |
|
$json = $serializer->normalize( |
34
|
|
|
$surveys, |
35
|
2 |
|
null, |
36
|
2 |
|
array('groups' => array('group1')) |
37
|
|
|
); |
38
|
|
|
|
39
|
2 |
|
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
|
|
|
$json = null; |
60
|
|
|
if ($survey->getStatus() == 'submited') { |
61
|
|
|
$json = $serializer->normalize( |
62
|
|
|
$survey, |
63
|
|
|
null, |
64
|
|
|
array('groups' => array('group1', 'group2')) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
if ($survey->getStatus() == 'current') { |
68
|
|
|
$json = $serializer->normalize( |
69
|
|
|
$survey, |
70
|
|
|
null, |
71
|
|
|
array('groups' => array('group1', 'group3')) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->json($json); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Request $request, Survey $survey |
|
|
|
|
80
|
|
|
* @Route("/survey/update/{id}", name="api_survey_update") |
81
|
|
|
* @Method("POST") |
82
|
|
|
* @ParamConverter("survey", class="AppBundle:Survey") |
83
|
|
|
*/ |
84
|
1 |
|
public function apiSurveyUpdateAction(Request $request, Survey $survey) |
85
|
|
|
{ |
86
|
1 |
|
$user = $this->getUser(); |
87
|
1 |
|
if (!$user) { |
88
|
|
|
return $this->json(['message' => 'User is not authorized'], 401); |
89
|
|
|
} |
90
|
1 |
|
if (!$survey || $survey->getStatus() == 'submited') { |
91
|
|
|
return $this->json(['message' => 'No survey'], 404); |
92
|
|
|
} |
93
|
1 |
|
$questKey = array(); |
94
|
1 |
|
foreach ($survey->getType()->getQuestions() as $question) { |
95
|
1 |
|
$questKey[] = $question->getId(); |
96
|
|
|
} |
97
|
1 |
|
$data = json_decode($request->getContent(), true); |
98
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
99
|
1 |
|
foreach ($questKey as $key) { |
100
|
1 |
|
$answer = $data[$key]; |
101
|
1 |
|
if ($answer == null) { |
102
|
|
|
return $this->json(['message' => 'Wrong question id'], 400); |
103
|
|
|
} |
104
|
1 |
|
$newAnswer = new SurveyAnswer(); |
105
|
1 |
|
$newAnswer->setSurvey($survey); |
106
|
1 |
|
$newAnswer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key)); |
|
|
|
|
107
|
1 |
|
$newAnswer->setContent($answer); |
108
|
1 |
|
$em->persist($newAnswer); |
109
|
|
|
} |
110
|
1 |
|
$survey->setStatus('submited'); |
111
|
1 |
|
$em->flush(); |
112
|
|
|
|
113
|
1 |
|
return $this->json(['message' => 'Survey updated'], 200); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.