1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Rest/Describer/Response.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace App\Rest\Describer; |
9
|
|
|
|
10
|
|
|
use App\Rest\Controller; |
11
|
|
|
use App\Rest\Doc\RouteModel; |
12
|
|
|
use EXSyst\Component\Swagger\Operation; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Response |
17
|
|
|
* |
18
|
|
|
* @package App\Rest\Describer |
19
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Response |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ContainerInterface |
25
|
|
|
*/ |
26
|
|
|
private $container; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Responses |
30
|
|
|
*/ |
31
|
|
|
private $responses; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Response constructor. |
35
|
|
|
* |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
* @param Responses $responses |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(ContainerInterface $container, Responses $responses) |
40
|
|
|
{ |
41
|
2 |
|
$this->container = $container; |
42
|
2 |
|
$this->responses = $responses; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Operation $operation |
47
|
|
|
* @param RouteModel $routeModel |
48
|
|
|
* |
49
|
|
|
* @throws \UnexpectedValueException |
50
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
51
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
52
|
|
|
*/ |
53
|
2 |
|
public function process(Operation $operation, RouteModel $routeModel): void |
54
|
|
|
{ |
55
|
2 |
|
$description = ''; |
56
|
2 |
|
$statusCode = 200; |
57
|
2 |
|
$responses = []; |
58
|
|
|
|
59
|
2 |
|
switch ($routeModel->getMethod()) { |
60
|
2 |
|
case Rest::COUNT_ACTION: |
61
|
2 |
|
$description = 'Count of (%s) entities'; |
62
|
2 |
|
break; |
63
|
2 |
|
case Rest::CREATE_ACTION: |
64
|
2 |
|
$description = 'Created new entity (%s)'; |
65
|
2 |
|
$statusCode = 201; |
66
|
2 |
|
break; |
67
|
2 |
|
case Rest::DELETE_ACTION: |
68
|
2 |
|
$description = 'Deleted entity (%s)'; |
69
|
2 |
|
$responses[] = 'add404'; |
70
|
2 |
|
break; |
71
|
2 |
|
case Rest::FIND_ACTION: |
72
|
2 |
|
$description = 'Array of fetched entities (%s)'; |
73
|
2 |
|
break; |
74
|
2 |
|
case Rest::FIND_ONE_ACTION: |
75
|
2 |
|
$description = 'Fetched entity (%s)'; |
76
|
2 |
|
$responses[] = 'add404'; |
77
|
2 |
|
break; |
78
|
2 |
|
case Rest::IDS_ACTION: |
79
|
2 |
|
$description = 'Fetched entities (%s) primary key values'; |
80
|
2 |
|
break; |
81
|
2 |
|
case Rest::PATCH_ACTION: |
82
|
2 |
|
$description = 'Deleted entity (%s)'; |
83
|
2 |
|
$responses[] = 'add404'; |
84
|
2 |
|
break; |
85
|
2 |
|
case Rest::UPDATE_ACTION: |
86
|
2 |
|
$description = 'Updated entity (%s)'; |
87
|
2 |
|
$responses[] = 'add404'; |
88
|
2 |
|
break; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$this->processResponse($operation, $routeModel, $description, $statusCode, $responses); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Operation $operation |
96
|
|
|
* @param RouteModel $routeModel |
97
|
|
|
* @param string $description |
98
|
|
|
* @param int $statusCode |
99
|
|
|
* @param array $responses |
100
|
|
|
* |
101
|
|
|
* @throws \UnexpectedValueException |
102
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
103
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
104
|
|
|
*/ |
105
|
2 |
|
private function processResponse( |
106
|
|
|
Operation $operation, |
107
|
|
|
RouteModel $routeModel, |
108
|
|
|
string $description, |
109
|
|
|
int $statusCode, |
110
|
|
|
array $responses |
111
|
|
|
): void { |
112
|
2 |
|
if (!empty($description) && $this->container->has($routeModel->getController())) { |
113
|
|
|
/** @var Controller $controller */ |
114
|
2 |
|
$controller = $this->container->get($routeModel->getController()); |
115
|
|
|
|
116
|
2 |
|
$this->responses->addOk($operation, $description, $statusCode, $controller->getResource()->getEntityName()); |
117
|
|
|
|
118
|
2 |
|
foreach ($responses as $method) { |
119
|
2 |
|
$this->responses->$method($operation, $routeModel); |
120
|
|
|
} |
121
|
|
|
} |
122
|
2 |
|
} |
123
|
|
|
} |
124
|
|
|
|