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\CollectionApiResponse; |
9
|
|
|
use AppBundle\Response\FileResponse; |
10
|
|
|
use AppBundle\Response\Infrastructure\AbstractApiResponse; |
11
|
|
|
use AppBundle\Response\Infrastructure\HttpLocationInterface; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\Form\FormBuilder; |
14
|
|
|
use Symfony\Component\Form\FormInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Vehsamrak |
20
|
|
|
*/ |
21
|
|
|
class RestController extends Controller |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
const MIME_TYPE_JSON = 'application/json'; |
25
|
|
|
|
26
|
37 |
|
public function respond(AbstractApiResponse $apiResponse): Response |
27
|
|
|
{ |
28
|
37 |
|
$response = $this->get('rockparade.http_response_factory')->createResponse($apiResponse); |
29
|
|
|
|
30
|
37 |
|
$this->setLocation($response, $apiResponse); |
31
|
37 |
|
$this->setContentType($apiResponse, $response); |
32
|
|
|
|
33
|
37 |
|
return $response; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Process form |
38
|
|
|
* @param Request $request |
39
|
|
|
* @param FormInterface $form |
40
|
|
|
*/ |
41
|
13 |
|
protected function processForm(Request $request, FormInterface $form) |
42
|
|
|
{ |
43
|
13 |
|
$formData = json_decode($request->getContent(), true) ?? $request->request->all(); |
44
|
13 |
|
$clearMissing = $request->getMethod() != Request::METHOD_PATCH; |
45
|
|
|
|
46
|
13 |
|
$form->submit($formData, $clearMissing); |
47
|
13 |
|
} |
48
|
|
|
|
49
|
|
|
/** {@inheritDoc} */ |
50
|
10 |
|
protected function createFormBuilder($data = null, array $options = []): FormBuilder |
51
|
|
|
{ |
52
|
10 |
|
$options['allow_extra_fields'] = true; |
53
|
|
|
|
54
|
10 |
|
return parent::createFormBuilder($data, $options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create collection api response with total, limit and offset parameters |
59
|
|
|
* @param integer|null $limit |
60
|
|
|
* @param integer|null $offset |
61
|
|
|
*/ |
62
|
4 |
|
protected function createCompleteCollectionResponse(AbstractRepository $repository, $limit, $offset): CollectionApiResponse |
63
|
|
|
{ |
64
|
4 |
|
$limit = (int) filter_var($limit, FILTER_VALIDATE_INT); |
65
|
4 |
|
$offset = (int) filter_var($offset, FILTER_VALIDATE_INT); |
66
|
|
|
|
67
|
4 |
|
$entities = $repository->findAllWithLimitAndOffset($limit, $offset); |
68
|
4 |
|
$entitiesQuantity = $repository->countAll(); |
69
|
|
|
|
70
|
4 |
|
$response = new CollectionApiResponse($entities, Response::HTTP_OK, $entitiesQuantity, $limit, $offset); |
71
|
|
|
|
72
|
4 |
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $entityFullName Entity class name |
77
|
|
|
* @param string|int $id Entity id |
78
|
|
|
* @return ApiError |
79
|
|
|
* @throws EntityNotFoundException |
80
|
|
|
*/ |
81
|
2 |
|
protected function createEntityNotFoundResponse(string $entityFullName, $id): ApiError |
82
|
|
|
{ |
83
|
2 |
|
$entityName = (new \ReflectionClass($entityFullName))->getShortName(); |
84
|
|
|
|
85
|
2 |
|
return new ApiError( |
86
|
2 |
|
sprintf('%s "%s" was not found.', $entityName, $id), |
87
|
2 |
|
Response::HTTP_NOT_FOUND |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
37 |
|
private function setLocation(Response $response, AbstractApiResponse $apiResponse) |
92
|
|
|
{ |
93
|
37 |
|
if ($apiResponse instanceof HttpLocationInterface) { |
94
|
4 |
|
$response->headers->set('Location', $apiResponse->getLocation()); |
95
|
|
|
} |
96
|
37 |
|
} |
97
|
|
|
|
98
|
37 |
|
private function setContentType(AbstractApiResponse $apiResponse, Response $response) |
99
|
|
|
{ |
100
|
37 |
|
if (!$apiResponse instanceof FileResponse) { |
101
|
37 |
|
$response->headers->set('Content-Type', self::MIME_TYPE_JSON); |
102
|
|
|
} |
103
|
37 |
|
} |
104
|
|
|
} |
105
|
|
|
|