1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Infrastructure; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Infrasctucture\AbstractRepository; |
6
|
|
|
use AppBundle\Exception\EntityNotFoundException; |
7
|
|
|
use AppBundle\Response\ApiError; |
8
|
|
|
use AppBundle\Response\ApiResponse; |
9
|
|
|
use AppBundle\Response\CollectionApiResponse; |
10
|
|
|
use AppBundle\Response\FileResponse; |
11
|
|
|
use AppBundle\Response\Infrastructure\AbstractApiResponse; |
12
|
|
|
use AppBundle\Response\Infrastructure\HttpLocationInterface; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
14
|
|
|
use Symfony\Component\Form\FormBuilder; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Vehsamrak |
21
|
|
|
*/ |
22
|
|
|
class RestController extends Controller |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
const MIME_TYPE_JSON = 'application/json'; |
26
|
|
|
|
27
|
39 |
|
public function respond(AbstractApiResponse $apiResponse): Response |
28
|
|
|
{ |
29
|
39 |
|
$response = $this->get('rockparade.http_response_factory')->createResponse($apiResponse); |
30
|
|
|
|
31
|
39 |
|
$this->setLocation($response, $apiResponse); |
32
|
39 |
|
$this->setContentType($apiResponse, $response); |
33
|
|
|
|
34
|
39 |
|
return $response; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Process form |
39
|
|
|
* @param Request $request |
40
|
|
|
* @param FormInterface $form |
41
|
|
|
*/ |
42
|
13 |
|
protected function processForm(Request $request, FormInterface $form) |
43
|
|
|
{ |
44
|
13 |
|
$formData = json_decode($request->getContent(), true) ?? $request->request->all(); |
45
|
13 |
|
$clearMissing = $request->getMethod() != Request::METHOD_PATCH; |
46
|
|
|
|
47
|
13 |
|
$form->submit($formData, $clearMissing); |
48
|
13 |
|
} |
49
|
|
|
|
50
|
|
|
/** {@inheritDoc} */ |
51
|
10 |
|
protected function createFormBuilder($data = null, array $options = []): FormBuilder |
52
|
|
|
{ |
53
|
10 |
|
$options['allow_extra_fields'] = true; |
54
|
|
|
|
55
|
10 |
|
return parent::createFormBuilder($data, $options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create collection api response with total, limit and offset parameters |
60
|
|
|
* @param integer|null $limit |
61
|
|
|
* @param integer|null $offset |
62
|
|
|
*/ |
63
|
4 |
|
protected function createCompleteCollectionResponse(AbstractRepository $repository, $limit, $offset): CollectionApiResponse |
64
|
|
|
{ |
65
|
4 |
|
$limit = (int) filter_var($limit, FILTER_VALIDATE_INT); |
66
|
4 |
|
$offset = (int) filter_var($offset, FILTER_VALIDATE_INT); |
67
|
|
|
|
68
|
4 |
|
$entities = $repository->findAllWithLimitAndOffset($limit, $offset); |
69
|
4 |
|
$entitiesQuantity = $repository->countAll(); |
70
|
|
|
|
71
|
4 |
|
$response = new CollectionApiResponse($entities, Response::HTTP_OK, $entitiesQuantity, $limit, $offset); |
72
|
|
|
|
73
|
4 |
|
return $response; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $entityFullName Entity class name |
78
|
|
|
* @param string|int $id Entity id |
79
|
|
|
* @return ApiError |
80
|
|
|
* @throws EntityNotFoundException |
81
|
|
|
*/ |
82
|
5 |
|
protected function createEntityNotFoundResponse(string $entityFullName, $id): ApiError |
83
|
|
|
{ |
84
|
5 |
|
return $this->get('rockparade.entity_service')->createEntityNotFoundResponse($entityFullName, $id); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string|int $id |
89
|
|
|
*/ |
90
|
13 |
View Code Duplication |
protected function viewEntity(AbstractRepository $repository, $id): Response |
|
|
|
|
91
|
|
|
{ |
92
|
13 |
|
$entity = $repository->findOneById($id); |
93
|
13 |
|
$entityClass = $repository->getClassName(); |
94
|
|
|
|
95
|
13 |
|
if ($entity) { |
96
|
9 |
|
$response = new ApiResponse($entity, Response::HTTP_OK); |
97
|
|
|
} else { |
98
|
5 |
|
$response = $this->createEntityNotFoundResponse($entityClass, $id); |
99
|
|
|
} |
100
|
|
|
|
101
|
13 |
|
return $this->respond($response); |
102
|
|
|
} |
103
|
|
|
|
104
|
39 |
|
private function setLocation(Response $response, AbstractApiResponse $apiResponse) |
105
|
|
|
{ |
106
|
39 |
|
if ($apiResponse instanceof HttpLocationInterface) { |
107
|
4 |
|
$response->headers->set('Location', $apiResponse->getLocation()); |
108
|
|
|
} |
109
|
39 |
|
} |
110
|
|
|
|
111
|
39 |
|
private function setContentType(AbstractApiResponse $apiResponse, Response $response) |
112
|
|
|
{ |
113
|
39 |
|
if (!$apiResponse instanceof FileResponse) { |
114
|
39 |
|
$response->headers->set('Content-Type', self::MIME_TYPE_JSON); |
115
|
|
|
} |
116
|
39 |
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.