|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Response; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\DocumentRepository; |
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\Specification; |
|
13
|
|
|
use KleijnWeb\SwaggerBundle\Request\RequestMeta; |
|
14
|
|
|
use KleijnWeb\SwaggerBundle\Serialize\Serializer; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author John Kleijn <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ResponseFactory |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Serializer |
|
25
|
|
|
*/ |
|
26
|
|
|
private $serializer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var DocumentRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $documentRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param DocumentRepository $documentRepository |
|
35
|
|
|
* @param Serializer $serializer |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
DocumentRepository $documentRepository, |
|
39
|
|
|
Serializer $serializer |
|
40
|
|
|
) { |
|
41
|
|
|
$this->serializer = $serializer; |
|
42
|
|
|
$this->documentRepository = $documentRepository; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
47
|
|
|
* |
|
48
|
|
|
* @param Request $request |
|
49
|
|
|
* @param mixed $data |
|
50
|
|
|
* |
|
51
|
|
|
* @return Response |
|
52
|
|
|
*/ |
|
53
|
|
|
public function createResponse(Request $request, $data) |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var RequestMeta $meta */ |
|
56
|
|
|
$meta = $request->attributes->get('_swagger.meta'); |
|
57
|
|
|
$specification = $meta->getSpecification(); |
|
58
|
|
|
|
|
59
|
|
|
if ($data !== null) { |
|
60
|
|
|
$data = $this->serializer->serialize($data, $meta->getDefinitionMap()); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$operation = $specification |
|
64
|
|
|
->getOperation( |
|
65
|
|
|
$request->get('_swagger.path'), |
|
66
|
|
|
$request->getMethod() |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$responseCode = 200; |
|
70
|
|
|
$understands204 = false; |
|
71
|
|
|
foreach ($operation->getResponseCodes() as $statusCode) { |
|
72
|
|
|
if ($statusCode == 204) { |
|
73
|
|
|
$understands204 = true; |
|
74
|
|
|
} |
|
75
|
|
|
if (2 == substr($statusCode, 0, 1)) { |
|
76
|
|
|
$responseCode = $statusCode; |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($data === null && $understands204) { |
|
82
|
|
|
$responseCode = 204; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return new Response($data, $responseCode, ['Content-Type' => 'application/json']); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: