|
1
|
|
|
<?php |
|
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\Serializer\SerializerAdapter; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author John Kleijn <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ResponseFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var SerializerAdapter |
|
23
|
|
|
*/ |
|
24
|
|
|
private $serializer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var DocumentRepository |
|
28
|
|
|
*/ |
|
29
|
|
|
private $documentRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param DocumentRepository $documentRepository |
|
33
|
|
|
* @param SerializerAdapter $serializer |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(DocumentRepository $documentRepository, SerializerAdapter $serializer) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->serializer = $serializer; |
|
38
|
|
|
$this->documentRepository = $documentRepository; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
43
|
|
|
* |
|
44
|
|
|
* @param Request $request |
|
45
|
|
|
* @param mixed $data |
|
46
|
|
|
* |
|
47
|
|
|
* @return Response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function createResponse(Request $request, $data) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$request->get('_definition')) { |
|
52
|
|
|
throw new \LogicException("Request does not contain reference to definition"); |
|
53
|
|
|
} |
|
54
|
|
|
if (!$request->get('_swagger_path')) { |
|
55
|
|
|
throw new \LogicException("Request does not contain reference to Swagger path"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($data !== null) { |
|
59
|
|
|
$data = $this->serializer->serialize($data, 'json'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$swaggerDocument = $this->documentRepository->get($request->get('_definition')); |
|
63
|
|
|
|
|
64
|
|
|
$operationDefinition = $swaggerDocument |
|
|
|
|
|
|
65
|
|
|
->getOperationDefinition( |
|
66
|
|
|
$request->get('_swagger_path'), |
|
67
|
|
|
$request->getMethod() |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$responseCode = 200; |
|
71
|
|
|
$understands204 = false; |
|
72
|
|
|
foreach (array_keys((array)$operationDefinition->responses) as $statusCode) { |
|
73
|
|
|
if ($statusCode == 204) { |
|
74
|
|
|
$understands204 = true; |
|
75
|
|
|
} |
|
76
|
|
|
if (2 == substr($statusCode, 0, 1)) { |
|
77
|
|
|
$responseCode = $statusCode; |
|
78
|
|
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($data === null && $understands204) { |
|
83
|
|
|
$responseCode = 204; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return new Response($data, $responseCode, ['Content-Type' => 'application/json']); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This method has been deprecated.