1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IncidentBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use IncidentBundle\Entity\Incident; |
7
|
|
|
use JMS\Serializer\Serializer; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class IncidentController |
16
|
|
|
*/ |
17
|
|
|
class IncidentController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Serializer; |
21
|
|
|
*/ |
22
|
|
|
private $serializer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @Route("/{ident}") |
26
|
|
|
* @Method({"GET"}) |
27
|
|
|
* |
28
|
|
|
* @param int $ident |
29
|
|
|
* |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
2 |
|
public function getAction($ident) |
33
|
|
|
{ |
34
|
2 |
|
$errors = []; |
35
|
2 |
|
set_error_handler($this->getErrorHandlerCallback($errors)); |
36
|
|
|
|
37
|
2 |
|
$responseData = ['data' => [], 'error' => []]; |
38
|
|
|
try { |
39
|
2 |
|
if ($incident = $this->getIncident($ident, $errors)) { |
40
|
|
|
/** @var Serializer $serializer */ |
41
|
1 |
|
$serializer = $this->get('jms_serializer'); |
42
|
2 |
|
$responseData['data'] = $serializer->toArray($incident); |
43
|
|
|
} |
44
|
|
|
} catch (Exception $e) { |
45
|
|
|
$errors[] = $e; |
46
|
|
|
} |
47
|
2 |
|
$responseData['errors'] = $errors; |
48
|
|
|
|
49
|
2 |
|
return new Response(json_encode($responseData)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @Route("/{ident}") |
54
|
|
|
* @Method({"POST"}) |
55
|
|
|
* |
56
|
|
|
* @param int $ident |
57
|
|
|
* @param Request $request |
58
|
|
|
* |
59
|
|
|
* @return Response |
60
|
|
|
*/ |
61
|
2 |
|
public function postAction($ident, Request $request) |
62
|
|
|
{ |
63
|
2 |
|
$errors = []; |
64
|
2 |
|
set_error_handler($this->getErrorHandlerCallback($errors)); |
65
|
|
|
|
66
|
2 |
|
$responseData = ['data' => [], 'errors' => []]; |
67
|
|
|
try { |
68
|
2 |
|
if ($incident = $this->getIncident($ident, $errors)) { |
69
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
70
|
|
|
/** @var Serializer $serializer */ |
71
|
1 |
|
$serializer = $this->get('jms_serializer'); |
72
|
1 |
|
$incidentNew = $serializer->deserialize($request->getContent(), Incident::class, 'json'); |
|
|
|
|
73
|
1 |
|
$incidentNew->setId($incident->getId()); |
74
|
1 |
|
$entityManager->merge($incidentNew); |
75
|
1 |
|
$entityManager->flush(); |
76
|
|
|
|
77
|
2 |
|
$responseData['data'] = $this->get('jms_serializer')->toArray($incident); |
78
|
|
|
} |
79
|
|
|
} catch (Exception $e) { |
80
|
|
|
$errors[] = $e; |
81
|
|
|
} |
82
|
2 |
|
$responseData['errors'] = $errors; |
83
|
|
|
|
84
|
2 |
|
return new Response(json_encode($responseData)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @Route("/") |
89
|
|
|
* @Method({"PUT"}) |
90
|
|
|
* |
91
|
|
|
* @param Request $request |
92
|
|
|
* |
93
|
|
|
* @return Response |
94
|
|
|
*/ |
95
|
2 |
|
public function putAction(Request $request) |
96
|
|
|
{ |
97
|
2 |
|
$errors = []; |
98
|
2 |
|
set_error_handler($this->getErrorHandlerCallback($errors)); |
99
|
|
|
|
100
|
2 |
|
$responseData = ['data' => [], 'errors' => []]; |
101
|
|
|
try { |
102
|
|
|
|
103
|
2 |
|
$entityManager = $this->getDoctrine()->getManager(); |
104
|
|
|
/** @var Serializer $serializer */ |
105
|
2 |
|
$serializer = $this->get('jms_serializer'); |
106
|
|
|
/** @var Incident $incident */ |
107
|
2 |
|
$incident = $serializer->deserialize($request->getContent(), Incident::class, 'json'); |
|
|
|
|
108
|
|
|
|
109
|
2 |
|
$entityManager->persist($incident); |
110
|
|
|
|
111
|
2 |
|
$entityManager->flush(); |
112
|
|
|
|
113
|
|
|
|
114
|
1 |
|
$responseData['data'] = $this->get('jms_serializer')->toArray($incident); |
115
|
1 |
|
} catch (Exception $e) { |
116
|
1 |
|
$errors[] = $e->getMessage(); |
117
|
|
|
} |
118
|
2 |
|
$responseData['errors'] = $errors; |
119
|
|
|
|
120
|
2 |
|
return new Response(json_encode($responseData)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @Route("/{ident}") |
125
|
|
|
* @Method({"DELETE"}) |
126
|
|
|
* |
127
|
|
|
* @param int $ident |
128
|
|
|
* |
129
|
|
|
* @return Response |
130
|
|
|
*/ |
131
|
2 |
|
public function deleteAction($ident) |
132
|
|
|
{ |
133
|
2 |
|
$errors = []; |
134
|
2 |
|
set_error_handler($this->getErrorHandlerCallback($errors)); |
135
|
|
|
|
136
|
2 |
|
$responseData = ['data' => [], 'errors' => []]; |
137
|
|
|
try { |
138
|
2 |
|
if ($incident = $this->getIncident($ident, $errors)) { |
139
|
1 |
|
$entityManager = $this->getDoctrine()->getManager(); |
140
|
1 |
|
$entityManager->remove($incident); |
141
|
2 |
|
$entityManager->flush(); |
142
|
|
|
} |
143
|
|
|
} catch (Exception $e) { |
144
|
|
|
$errors[] = $e; |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
$responseData['errors'] = $errors; |
148
|
|
|
|
149
|
2 |
|
return new Response(json_encode($responseData)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return Serializer |
154
|
|
|
*/ |
155
|
|
|
public function getSerializer() |
156
|
|
|
{ |
157
|
|
|
return $this->serializer; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Serializer $serializer |
162
|
|
|
*/ |
163
|
|
|
protected function setSerializer(Serializer $serializer) |
164
|
|
|
{ |
165
|
|
|
$this->serializer = $serializer; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $ident |
170
|
|
|
* @param array $errors |
171
|
|
|
* |
172
|
|
|
* @return Incident |
173
|
|
|
*/ |
174
|
6 |
|
protected function getIncident($ident, &$errors) |
175
|
|
|
{ |
176
|
6 |
|
$incident = $this->getDoctrine() |
177
|
6 |
|
->getRepository('IncidentBundle:Incident') |
178
|
6 |
|
->findOneBy(['ident' => $ident], ['id' => 'desc']); |
|
|
|
|
179
|
6 |
|
if (!$incident) { |
180
|
4 |
|
$errors[] = ['code' => 404, 'text' => 'Incident not find']; |
181
|
|
|
} |
182
|
|
|
|
183
|
6 |
|
return $incident; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $errors |
188
|
|
|
* |
189
|
|
|
* @return \Closure |
190
|
|
|
*/ |
191
|
|
|
protected function getErrorHandlerCallback(&$errors) |
192
|
|
|
{ |
193
|
7 |
|
return function ($errno, $errstr, $errfile, $errline) use (&$errors) { |
|
|
|
|
194
|
|
|
$errors[] = ['code' => $errno, 'text' => $errstr]; |
195
|
7 |
|
}; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.