|
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
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
13
|
|
|
|
|
14
|
|
|
class SurveyController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @Route("/surveys", name="list_surveys") |
|
18
|
|
|
* @Method("GET") |
|
19
|
|
|
* |
|
20
|
|
|
* @return JsonResponse |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function listAction() |
|
23
|
|
|
{ |
|
24
|
2 |
|
$surveys = $this->getDoctrine() |
|
25
|
2 |
|
->getRepository(Survey::class) |
|
26
|
2 |
|
->findSurveyByUser($this->getUser()); |
|
27
|
2 |
|
$json = $this->get('serializer')->normalize($surveys, null, array('groups' => array('list'))); |
|
28
|
|
|
|
|
29
|
2 |
|
return $this->json(['surveys' => $json], 200); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int $id |
|
34
|
|
|
* @Route("/surveys/{id}", requirements={"id": "\d+"}, name="show_survey") |
|
35
|
|
|
* @Method("GET") |
|
36
|
|
|
* |
|
37
|
|
|
* @return JsonResponse |
|
38
|
|
|
*/ |
|
39
|
|
|
public function showAction($id) |
|
40
|
|
|
{ |
|
41
|
|
|
$user = $this->getUser(); |
|
42
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
43
|
|
|
$survey = $em->getRepository(Survey::class)->find($id); |
|
44
|
|
|
if ($user !== $survey->getUser()) { |
|
45
|
|
|
throw new JsonHttpException(403, "Current user doesn't have accesses to this resource"); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->json(['survey' => $survey], 200); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Request $request, int $id |
|
|
|
|
|
|
53
|
|
|
* @Route("/surveys/{id}", requirements={"id": "\d+"}, name="edit_survey") |
|
54
|
|
|
* @Method("PUT") |
|
55
|
|
|
* |
|
56
|
|
|
* @return JsonResponse |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function editAction(Request $request, $id) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$user = $this->getUser(); |
|
61
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
62
|
1 |
|
$data = $request->getContent(); |
|
63
|
1 |
|
if (!$data) { |
|
64
|
|
|
throw new JsonHttpException(400, 'Bad Request.'); |
|
65
|
|
|
} |
|
66
|
1 |
|
$survey = $em->getRepository(Survey::class)->find($id); |
|
67
|
|
|
|
|
68
|
1 |
|
if ($user !== $survey->getUser()) { |
|
69
|
|
|
throw new JsonHttpException(403, "Current user can't change this resource"); |
|
70
|
|
|
} |
|
71
|
1 |
|
if ('current' !== $survey->getStatus()) { |
|
72
|
|
|
throw new JsonHttpException(404, 'Survey was already submited'); |
|
73
|
|
|
} |
|
74
|
1 |
|
$serializer = $this->get('serializer'); |
|
75
|
|
|
|
|
76
|
1 |
|
$survey = $serializer->deserialize( |
|
77
|
|
|
$data, |
|
78
|
1 |
|
Survey::class, |
|
79
|
1 |
|
'json', |
|
80
|
1 |
|
array('object_to_populate' => $survey) |
|
81
|
|
|
); |
|
82
|
1 |
|
$errors = $this->get('validator')->validate($survey); |
|
83
|
1 |
View Code Duplication |
if ($errors->count()) { |
|
|
|
|
|
|
84
|
|
|
$outErrors = []; |
|
85
|
|
|
/** @var ConstraintViolation $error */ |
|
86
|
|
|
foreach ($errors as $error) { |
|
87
|
|
|
$outErrors[$error->getPropertyPath()] = $error->getMessage(); |
|
88
|
|
|
} |
|
89
|
|
|
throw new JsonHttpException(400, 'Bad Request', $outErrors); |
|
90
|
|
|
} |
|
91
|
1 |
|
$em->persist($survey); |
|
92
|
1 |
|
$em->flush(); |
|
93
|
|
|
|
|
94
|
1 |
|
return $this->json(['message' => 'Survey is updated'], 200); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.