|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Infrastructure; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Infrasctucture\AbstractRepository; |
|
6
|
|
|
use AppBundle\Enum\ApiOperation; |
|
7
|
|
|
use AppBundle\Exception\EntityNotFoundException; |
|
8
|
|
|
use AppBundle\Form\FormOptions; |
|
9
|
|
|
use AppBundle\Response\ApiError; |
|
10
|
|
|
use AppBundle\Response\ApiResponse; |
|
11
|
|
|
use AppBundle\Response\CollectionApiResponse; |
|
12
|
|
|
use AppBundle\Response\FileResponse; |
|
13
|
|
|
use AppBundle\Response\Infrastructure\AbstractApiResponse; |
|
14
|
|
|
use AppBundle\Response\Infrastructure\HttpLocationInterface; |
|
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
16
|
|
|
use Symfony\Component\Form\FormBuilder; |
|
17
|
|
|
use Symfony\Component\Form\FormInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Vehsamrak |
|
23
|
|
|
*/ |
|
24
|
|
|
class RestController extends Controller |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
const MIME_TYPE_JSON = 'application/json'; |
|
28
|
|
|
|
|
29
|
41 |
|
public function respond(AbstractApiResponse $apiResponse): Response |
|
30
|
|
|
{ |
|
31
|
41 |
|
$response = $this->get('rockparade.http_response_factory')->createResponse($apiResponse); |
|
32
|
|
|
|
|
33
|
41 |
|
$this->setLocation($response, $apiResponse); |
|
34
|
41 |
|
$this->setContentType($apiResponse, $response); |
|
35
|
|
|
|
|
36
|
41 |
|
return $response; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
protected function createAndProcessForm( |
|
40
|
|
|
Request $request, |
|
41
|
|
|
string $type, |
|
42
|
|
|
$data = null, |
|
43
|
|
|
$options = [] |
|
44
|
|
|
) { |
|
45
|
4 |
|
$form = parent::createForm($type, $data, $options); |
|
|
|
|
|
|
46
|
4 |
|
$this->processForm($request, $form); |
|
47
|
|
|
|
|
48
|
4 |
|
return $form; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Process form |
|
53
|
|
|
* @param Request $request |
|
54
|
|
|
* @param FormInterface $form |
|
55
|
|
|
*/ |
|
56
|
15 |
|
protected function processForm(Request $request, FormInterface $form) |
|
57
|
|
|
{ |
|
58
|
15 |
|
$formData = json_decode($request->getContent(), true) ?? $request->request->all(); |
|
59
|
15 |
|
$clearMissing = $request->getMethod() != Request::METHOD_PATCH; |
|
60
|
|
|
|
|
61
|
15 |
|
$form->submit($formData, $clearMissing); |
|
62
|
15 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** {@inheritDoc} */ |
|
65
|
8 |
|
protected function createFormBuilder($data = null, array $options = []): FormBuilder |
|
66
|
|
|
{ |
|
67
|
8 |
|
$options['allow_extra_fields'] = true; |
|
68
|
|
|
|
|
69
|
8 |
|
return parent::createFormBuilder($data, $options); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create collection api response with total, limit and offset parameters |
|
74
|
|
|
* @param integer|null $limit |
|
75
|
|
|
* @param integer|null $offset |
|
76
|
|
|
*/ |
|
77
|
4 |
|
protected function listEntities(AbstractRepository $repository, $limit, $offset): Response |
|
78
|
|
|
{ |
|
79
|
4 |
|
$limit = (int) filter_var($limit, FILTER_VALIDATE_INT); |
|
80
|
4 |
|
$offset = (int) filter_var($offset, FILTER_VALIDATE_INT); |
|
81
|
|
|
|
|
82
|
4 |
|
$entities = $repository->findAllWithLimitAndOffset($limit, $offset); |
|
83
|
4 |
|
$entitiesQuantity = $repository->countAll(); |
|
84
|
|
|
|
|
85
|
4 |
|
$response = new CollectionApiResponse($entities, Response::HTTP_OK, $entitiesQuantity, $limit, $offset); |
|
86
|
|
|
|
|
87
|
4 |
|
return $this->respond($response); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $entityFullName Entity class name |
|
92
|
|
|
* @param string|int $id Entity id |
|
93
|
|
|
* @return ApiError |
|
94
|
|
|
* @throws EntityNotFoundException |
|
95
|
|
|
*/ |
|
96
|
5 |
|
protected function createEntityNotFoundResponse(string $entityFullName, $id): ApiError |
|
97
|
|
|
{ |
|
98
|
5 |
|
return $this->get('rockparade.entity_service')->createEntityNotFoundResponse($entityFullName, $id); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string|int $id |
|
103
|
|
|
*/ |
|
104
|
14 |
View Code Duplication |
protected function viewEntity(AbstractRepository $repository, $id): Response |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
14 |
|
$entity = $repository->findOneById($id); |
|
107
|
14 |
|
$entityClass = $repository->getClassName(); |
|
108
|
|
|
|
|
109
|
14 |
|
if ($entity) { |
|
110
|
10 |
|
$response = new ApiResponse($entity, Response::HTTP_OK); |
|
111
|
|
|
} else { |
|
112
|
5 |
|
$response = $this->createEntityNotFoundResponse($entityClass, $id); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
14 |
|
return $this->respond($response); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
41 |
|
private function setLocation(Response $response, AbstractApiResponse $apiResponse) |
|
119
|
|
|
{ |
|
120
|
41 |
|
if ($apiResponse instanceof HttpLocationInterface) { |
|
121
|
5 |
|
$response->headers->set('Location', $apiResponse->getLocation()); |
|
122
|
|
|
} |
|
123
|
41 |
|
} |
|
124
|
|
|
|
|
125
|
41 |
|
private function setContentType(AbstractApiResponse $apiResponse, Response $response) |
|
126
|
|
|
{ |
|
127
|
41 |
|
if (!$apiResponse instanceof FileResponse) { |
|
128
|
41 |
|
$response->headers->set('Content-Type', self::MIME_TYPE_JSON); |
|
129
|
|
|
} |
|
130
|
41 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Create ApiOperation from Request method |
|
134
|
|
|
*/ |
|
135
|
4 |
|
protected function createApiOperation(Request $request): ApiOperation |
|
136
|
|
|
{ |
|
137
|
4 |
|
return new ApiOperation($request->getMethod()); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.