PluginProbe
Booking for Appointments and Events Calendar – Amelia / 2.4
Booking for Appointments and Events Calendar – Amelia v2.4
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 All 59 releases
← All changes | src/Application/Controller/Controller.php +9 -89 trunk2.4 View file →
@@ -2,25 +2,20 @@
2 2
3 3 namespace AmeliaBooking\Application\Controller;
4 4
5 5 use AmeliaBooking\Application\Commands\Command;
6 -use AmeliaBooking\Application\Commands\CommandResult;
7 -use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
8 6 use AmeliaBooking\Application\Services\User\UserApplicationService;
9 -use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
10 -use AmeliaBooking\Domain\Common\Exceptions\CustomException;
11 -use AmeliaBooking\Domain\Common\Exceptions\PaymentValidationException;
12 -use AmeliaBooking\Domain\Events\DomainEventBus;
13 7 use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
14 -use AmeliaBooking\Domain\Services\Logger\LoggerInterface;
15 8 use AmeliaBooking\Domain\Services\Permissions\PermissionsService;
16 9 use AmeliaBooking\Domain\Services\Settings\SettingsService;
17 10 use AmeliaBooking\Infrastructure\Common\Container;
11 +use AmeliaBooking\Domain\Events\DomainEventBus;
12 +use AmeliaBooking\Application\Commands\CommandResult;
18 13 use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage;
19 -use AmeliaBooking\Infrastructure\WP\Translations\FrontendStrings;
20 -use AmeliaVendor\Psr\Http\Message\ResponseInterface as Response;
21 -use AmeliaVendor\Psr\Http\Message\ServerRequestInterface as Request;
14 +use AmeliaBooking\Domain\Common\Exceptions\CustomException;
22 15 use League\Tactician\CommandBus;
16 +use Slim\Http\Request;
17 +use Slim\Http\Response;
23 18
24 19 /**
25 20 * Class Controller
26 21 *
@@ -47,13 +42,8 @@
47 42 /**
48 43 * @var PermissionsService
49 44 */
50 45 protected $permissionsService;
51 -
52 - /**
53 - * @var LoggerInterface
54 - */
55 - protected $logger;
56 46 protected $allowedFields = [
57 47 'ameliaNonce',
58 48 'wpAmeliaNonce',
59 49 ];
@@ -75,9 +65,8 @@
75 65 $this->commandBus = $container->getCommandBus();
76 66 $this->eventBus = $container->getEventBus();
77 67 $this->permissionsService = $fromApi ? $container->getApiPermissionsService() : $container->getPermissionsService();
78 68 $this->userApplicationService = $fromApi ? $container->getApiUserApplicationService() : $container->getUserApplicationService();
79 - $this->logger = $container->getLoggerService()->channel(LoggerInterface::CHANNEL_HTTP);
80 69 }
81 70
82 71 /**
83 72 * @param Request $request
@@ -134,12 +123,8 @@
134 123 if (!$validApiCall && !$command->validateNonce($request)) {
135 124 return $response->withStatus(self::STATUS_FORBIDDEN);
136 125 }
137 126
138 - if (!$validApiCall && !$command->validateCron($request)) {
139 - return $response->withStatus(self::STATUS_FORBIDDEN);
140 - }
141 -
142 127 $command->setPermissionService($this->permissionsService);
143 128 $command->setUserApplicationService($this->userApplicationService);
144 129
145 130 try {
@@ -144,47 +129,13 @@
144 129
145 130 try {
146 131 /** @var CommandResult $commandResult */
147 132 $commandResult = $this->commandBus->handle($command);
148 - } catch (PaymentValidationException $e) {
149 - $commandResult = new CommandResult();
150 -
151 - $commandResult->setResult(CommandResult::RESULT_ERROR);
152 - $commandResult->setMessage(FrontendStrings::getCommonStrings()['payment_error']);
153 - $commandResult->setData(
154 - [
155 - 'paymentSuccessful' => false,
156 - ]
157 - );
158 - } catch (AccessDeniedException $e) {
159 - $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
160 - $response = $response->withStatus(self::STATUS_FORBIDDEN);
161 -
162 - $response->getBody()->write(
163 - json_encode(
164 - [
165 - 'data' => [
166 - 'message' => $e->getMessage()
167 - ]
168 - ]
169 - )
170 - );
171 -
172 - return $response;
173 133 } catch (CustomException $e) {
174 - try {
175 - $this->logger->error('Unhandled exception in controller', [
176 - 'command' => get_class($command),
177 - 'exception' => $e,
178 - ]);
179 - } catch (\Throwable $loggingError) {
180 - // Telemetry must not block the JSON 500 response.
181 - }
182 -
183 134 $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
184 135 $response = $response->withStatus(self::STATUS_INTERNAL_SERVER_ERROR);
185 136
186 - $response->getBody()->write(
137 + $response = $response->write(
187 138 json_encode(
188 139 [
189 140 'data' => [
190 141 'message' => $e->getMessage()
@@ -193,27 +144,10 @@
193 144 )
194 145 );
195 146
196 147 return $response;
197 - } catch (AuthorizationException $e) {
198 - $commandResult = new CommandResult();
199 -
200 - $commandResult->setResult(CommandResult::RESULT_ERROR);
201 - $commandResult->setData(
202 - [
203 - 'reauthorize' => true,
204 - ]
205 - );
206 148 }
207 149
208 - if (in_array($commandResult->getResult(), [CommandResult::RESULT_ERROR, CommandResult::RESULT_CONFLICT], true)) {
209 - $this->logger->warning('Command returned non-success result', [
210 - 'command' => get_class($command),
211 - 'result' => $commandResult->getResult(),
212 - 'message' => $commandResult->getMessage(),
213 - ]);
214 - }
215 -
216 150 if ($commandResult->getResult() === CommandResult::RESULT_ERROR) {
217 151 if ($settingsService->getSetting('activation', 'responseErrorAsConflict')) {
218 152 $commandResult->setResult(CommandResult::RESULT_CONFLICT);
219 153 }
@@ -253,10 +187,9 @@
253 187 }
254 188
255 189 /** @var Response $response */
256 190 $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
257 -
258 - $response->getBody()->write(
191 + $response = $response->write(
259 192 $this->sendJustData ? $commandResult->getData() :
260 193 json_encode(
261 194 $commandResult->hasDataInResponse() ?
262 195 $responseBody : array_merge($responseBody, ['data' => []])
@@ -284,10 +217,9 @@
284 217 }
285 218
286 219 $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
287 220 $response = $response->withHeader('Cache-Control', 'max-age=0');
288 -
289 - $response->getBody()->write($html);
221 + $response = $response->write($html);
290 222 }
291 223
292 224 if (($file = $commandResult->getFile()) !== null) {
293 225 /** @var Response $response */
@@ -298,9 +230,9 @@
298 230 if (array_key_exists('size', $file)) {
299 231 $response = $response->withHeader('Content-Length', $file['size']);
300 232 }
301 233
302 - $response->getBody()->write($file['content']);
234 + $response = $response->write($file['content']);
303 235 }
304 236
305 237 return $response;
306 238 }
@@ -431,18 +363,6 @@
431 363 'name' => $filename,
432 364 'size' => strlen($html),
433 365 'content' => $html,
434 366 ]);
435 - }
436 -
437 - /**
438 - * @param mixed $default
439 - *
440 - * @return mixed
441 - */
442 - public static function getParam(Request $request, string $key, $default = null)
443 - {
444 - $params = $request->getQueryParams();
445 -
446 - return array_key_exists($key, $params) ? $params[$key] : $default;
447 367 }
448 368 }