1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Api; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Survey\Survey; |
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
7
|
|
|
use AppBundle\Exception\JsonHttpException; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
class SurveyController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @Route("/surveys", name="list_surveys") |
17
|
|
|
* @Method("GET") |
18
|
|
|
* |
19
|
|
|
* @return JsonResponse |
20
|
|
|
*/ |
21
|
2 |
|
public function listAction() |
22
|
|
|
{ |
23
|
2 |
|
$surveys = $this->getDoctrine() |
24
|
2 |
|
->getRepository(Survey::class) |
25
|
2 |
|
->findSurveyByUser($this->getUser()); |
26
|
2 |
|
$json = $this->get('serializer')->normalize($surveys, null, array('groups' => array('list'))); |
27
|
|
|
|
28
|
2 |
|
return $this->json(['surveys' => $json], 200); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param int $id |
33
|
|
|
* @Route("/surveys/{id}", name="show_survey") |
34
|
|
|
* @Method("GET") |
35
|
|
|
* |
36
|
|
|
* @return JsonResponse |
37
|
|
|
*/ |
38
|
|
|
public function showAction($id) |
39
|
|
|
{ |
40
|
|
|
$user = $this->getUser(); |
41
|
|
|
$em = $this->getDoctrine()->getManager(); |
42
|
|
|
$survey = $em->getRepository(Survey::class)->find($id); |
43
|
|
|
if ($user !== $survey->getUser()) { |
44
|
|
|
throw new JsonHttpException(403, "Current user doesn't have accesses to this resource"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->json(['survey' => $survey], 200); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Request $request, int $id |
|
|
|
|
52
|
|
|
* @Route("/surveys/{id}", name="edit_survey") |
53
|
|
|
* @Method("PUT") |
54
|
|
|
* |
55
|
|
|
* @return JsonResponse |
56
|
|
|
*/ |
57
|
1 |
|
public function editAction(Request $request, $id) |
58
|
|
|
{ |
59
|
1 |
|
$user = $this->getUser(); |
60
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
61
|
1 |
|
$data = $request->getContent(); |
62
|
1 |
|
$survey = $em->getRepository(Survey::class)->find($id); |
63
|
|
|
|
64
|
1 |
|
if ($user !== $survey->getUser()) { |
65
|
|
|
throw new JsonHttpException(403, "Current user can't change this resource"); |
66
|
|
|
} |
67
|
1 |
|
if ('current' !== $survey->getStatus()) { |
68
|
|
|
throw new JsonHttpException(404, 'Survey was already submited'); |
69
|
|
|
} |
70
|
1 |
|
$serializer = $this->get('serializer'); |
71
|
|
|
|
72
|
1 |
|
$survey = $serializer->deserialize( |
73
|
|
|
$data, |
74
|
1 |
|
Survey::class, |
75
|
1 |
|
'json', |
76
|
1 |
|
array('object_to_populate' => $survey) |
77
|
|
|
); |
78
|
1 |
|
if (!$survey) { |
79
|
|
|
throw new JsonHttpException(404, 'Not valid Survey'); |
80
|
|
|
} |
81
|
1 |
|
$em->persist($survey); |
82
|
1 |
|
$em->flush(); |
83
|
|
|
|
84
|
1 |
|
return $this->json(['message' => 'Survey is updated'], 200); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.