Bookable
1 year ago
Booking
1 year ago
Entities
4 years ago
Import
4 years ago
Notification
1 year ago
Payment
1 year ago
Settings
1 year ago
Square
1 year ago
Stash
5 years ago
Stats
1 year ago
Test
2 years ago
User
1 year ago
WhatsNew
2 years ago
Controller.php
1 year ago
Controller.php
321 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Controller; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\Command; |
| 6 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 7 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 8 | use AmeliaBooking\Domain\Services\Permissions\PermissionsService; |
| 9 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 10 | use AmeliaBooking\Infrastructure\Common\Container; |
| 11 | use AmeliaBooking\Domain\Events\DomainEventBus; |
| 12 | use AmeliaBooking\Application\Commands\CommandResult; |
| 13 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 14 | use AmeliaBooking\Domain\Common\Exceptions\CustomException; |
| 15 | use League\Tactician\CommandBus; |
| 16 | use Slim\Http\Request; |
| 17 | use Slim\Http\Response; |
| 18 | |
| 19 | /** |
| 20 | * Class Controller |
| 21 | * |
| 22 | * @package AmeliaBooking\Application\Controller |
| 23 | */ |
| 24 | abstract class Controller |
| 25 | { |
| 26 | public const STATUS_OK = 200; |
| 27 | public const STATUS_REDIRECT = 302; |
| 28 | public const STATUS_FORBIDDEN = 403; |
| 29 | public const STATUS_NOT_FOUNT = 404; |
| 30 | public const STATUS_CONFLICT = 409; |
| 31 | public const STATUS_INTERNAL_SERVER_ERROR = 500; |
| 32 | |
| 33 | /** |
| 34 | * @var CommandBus |
| 35 | */ |
| 36 | protected $commandBus; |
| 37 | /** |
| 38 | * @var DomainEventBus |
| 39 | */ |
| 40 | protected $eventBus; |
| 41 | |
| 42 | /** |
| 43 | * @var PermissionsService |
| 44 | */ |
| 45 | protected $permissionsService; |
| 46 | protected $allowedFields = [ |
| 47 | 'ameliaNonce', |
| 48 | 'wpAmeliaNonce', |
| 49 | ]; |
| 50 | |
| 51 | protected $sendJustData = false; |
| 52 | /** |
| 53 | * @var UserApplicationService |
| 54 | */ |
| 55 | private $userApplicationService; |
| 56 | |
| 57 | /** |
| 58 | * Base Controller constructor. |
| 59 | * |
| 60 | * @param Container $container |
| 61 | * |
| 62 | * @throws \Interop\Container\Exception\ContainerException |
| 63 | */ |
| 64 | public function __construct(Container $container, $fromApi = false) |
| 65 | { |
| 66 | $this->commandBus = $container->getCommandBus(); |
| 67 | $this->eventBus = $container->getEventBus(); |
| 68 | $this->permissionsService = $fromApi ? $container->getApiPermissionsService() : $container->getPermissionsService(); |
| 69 | $this->userApplicationService = $fromApi ? $container->getApiUserApplicationService() : $container->getUserApplicationService(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param Request $request |
| 74 | * @param $args |
| 75 | * |
| 76 | * @return mixed |
| 77 | */ |
| 78 | abstract protected function instantiateCommand(Request $request, $args); |
| 79 | |
| 80 | /** |
| 81 | * Emit a success domain event, do nothing by default |
| 82 | * |
| 83 | * @param DomainEventBus $eventBus |
| 84 | * |
| 85 | * @param CommandResult $result |
| 86 | * |
| 87 | * @return null |
| 88 | */ |
| 89 | protected function emitSuccessEvent(DomainEventBus $eventBus, CommandResult $result) |
| 90 | { |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Emit a failure domain event, do nothing by default |
| 96 | * |
| 97 | * @param DomainEventBus $eventBus |
| 98 | * |
| 99 | * @param CommandResult $data |
| 100 | * |
| 101 | * @return null |
| 102 | */ |
| 103 | protected function emitFailureEvent(DomainEventBus $eventBus, CommandResult $data) |
| 104 | { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param Request $request |
| 110 | * @param Response $response |
| 111 | * @param $args |
| 112 | * |
| 113 | * @return Response |
| 114 | * @throws \InvalidArgumentException |
| 115 | * @throws \RuntimeException |
| 116 | */ |
| 117 | public function __invoke(Request $request, Response $response, $args, $validApiCall = false) |
| 118 | { |
| 119 | /** @var Command $command */ |
| 120 | $command = $this->instantiateCommand($request, $args); |
| 121 | |
| 122 | /** @var SettingsService $settingsService */ |
| 123 | $settingsService = new SettingsService(new SettingsStorage()); |
| 124 | |
| 125 | if (!$validApiCall && !$command->validateNonce($request)) { |
| 126 | return $response->withStatus(self::STATUS_FORBIDDEN); |
| 127 | } |
| 128 | |
| 129 | $command->setPermissionService($this->permissionsService); |
| 130 | $command->setUserApplicationService($this->userApplicationService); |
| 131 | |
| 132 | try { |
| 133 | /** @var CommandResult $commandResult */ |
| 134 | $commandResult = $this->commandBus->handle($command); |
| 135 | } catch (CustomException $e) { |
| 136 | $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8'); |
| 137 | $response = $response->withStatus(self::STATUS_INTERNAL_SERVER_ERROR); |
| 138 | |
| 139 | $response = $response->write( |
| 140 | json_encode( |
| 141 | [ |
| 142 | 'data' => [ |
| 143 | 'message' => $e->getMessage() |
| 144 | ] |
| 145 | ] |
| 146 | ) |
| 147 | ); |
| 148 | |
| 149 | return $response; |
| 150 | } |
| 151 | |
| 152 | if ($commandResult->getResult() === CommandResult::RESULT_ERROR) { |
| 153 | if ($settingsService->getSetting('activation', 'responseErrorAsConflict')) { |
| 154 | $commandResult->setResult(CommandResult::RESULT_CONFLICT); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if ($commandResult->getUrl() !== null) { |
| 159 | $this->emitSuccessEvent($this->eventBus, $commandResult); |
| 160 | |
| 161 | /** @var Response $response */ |
| 162 | $response = $response->withHeader('Location', $commandResult->getUrl()); |
| 163 | $response = $response->withStatus(self::STATUS_REDIRECT); |
| 164 | |
| 165 | return $response; |
| 166 | } |
| 167 | |
| 168 | if ($commandResult->hasAttachment() === false) { |
| 169 | $responseBody = [ |
| 170 | 'message' => $commandResult->getMessage(), |
| 171 | 'data' => $commandResult->getData() |
| 172 | ]; |
| 173 | |
| 174 | $this->emitSuccessEvent($this->eventBus, $commandResult); |
| 175 | |
| 176 | switch ($commandResult->getResult()) { |
| 177 | case (CommandResult::RESULT_SUCCESS): |
| 178 | $response = $response->withStatus(self::STATUS_OK); |
| 179 | |
| 180 | break; |
| 181 | case (CommandResult::RESULT_CONFLICT): |
| 182 | $response = $response->withStatus(self::STATUS_CONFLICT); |
| 183 | |
| 184 | break; |
| 185 | default: |
| 186 | $response = $response->withStatus(self::STATUS_INTERNAL_SERVER_ERROR); |
| 187 | |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | /** @var Response $response */ |
| 192 | $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8'); |
| 193 | $response = $response->write( |
| 194 | $this->sendJustData ? $commandResult->getData() : |
| 195 | json_encode( |
| 196 | $commandResult->hasDataInResponse() ? |
| 197 | $responseBody : array_merge($responseBody, ['data' => []]) |
| 198 | ) |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | if (($file = $commandResult->getFile()) !== null) { |
| 203 | /** @var Response $response */ |
| 204 | $response = $response->withHeader('Content-Type', $file['type']); |
| 205 | $response = $response->withHeader('Content-Disposition', 'inline; filename=' . '"' . $file['name'] . '"'); |
| 206 | $response = $response->withHeader('Cache-Control', 'max-age=0'); |
| 207 | |
| 208 | if (array_key_exists('size', $file)) { |
| 209 | $response = $response->withHeader('Content-Length', $file['size']); |
| 210 | } |
| 211 | |
| 212 | $response = $response->write($file['content']); |
| 213 | } |
| 214 | |
| 215 | return $response; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @param Command $command |
| 220 | * @param $requestBody |
| 221 | */ |
| 222 | protected function setCommandFields($command, $requestBody) |
| 223 | { |
| 224 | foreach ($this->allowedFields as $field) { |
| 225 | if (!isset($requestBody[$field])) { |
| 226 | continue; |
| 227 | } |
| 228 | $command->setField($field, $requestBody[$field]); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @param mixed $params |
| 234 | */ |
| 235 | protected function setArrayParams(&$params) |
| 236 | { |
| 237 | $names = [ |
| 238 | 'customers', |
| 239 | 'categories', |
| 240 | 'services', |
| 241 | 'packages', |
| 242 | 'employees', |
| 243 | 'providers', |
| 244 | 'providerIds', |
| 245 | 'locations', |
| 246 | 'events', |
| 247 | 'dates', |
| 248 | 'types', |
| 249 | 'fields', |
| 250 | 'statuses', |
| 251 | ]; |
| 252 | |
| 253 | foreach ($names as $name) { |
| 254 | if (!empty($params[$name])) { |
| 255 | $params[$name] = is_array($params[$name]) ? $params[$name] : explode(',', $params[$name]); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (isset($params['dates'][0])) { |
| 260 | $params['dates'][0] = preg_match("/^\d{4}-\d{2}-\d{2}$/", $params['dates'][0]) ? |
| 261 | $params['dates'][0] : DateTimeService::getNowDate(); |
| 262 | } |
| 263 | |
| 264 | if (isset($params['dates'][1])) { |
| 265 | $params['dates'][1] = preg_match("/^\d{4}-\d{2}-\d{2}$/", $params['dates'][1]) ? |
| 266 | $params['dates'][1] : DateTimeService::getNowDate(); |
| 267 | } |
| 268 | |
| 269 | if (isset($params['date'])) { |
| 270 | $params['date'] = preg_match("/^\d{4}-\d{2}-\d{2}$/", $params['date']) ? |
| 271 | $params['date'] : DateTimeService::getNowDate(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @param array $data |
| 277 | * @param string $field |
| 278 | * @param string $translationField |
| 279 | * |
| 280 | * @return void |
| 281 | */ |
| 282 | private function filterField(&$data, $field, $translationField) |
| 283 | { |
| 284 | if (!empty($data[$field])) { |
| 285 | global $allowedposttags; |
| 286 | |
| 287 | $data[$field] = wp_kses($data[$field], $allowedposttags); |
| 288 | |
| 289 | if (!empty($data['translations']) && ($translations = json_decode($data['translations'], true)) !== null) { |
| 290 | if (!empty($translations[$translationField])) { |
| 291 | foreach ($translations[$translationField] as $lang => $translation) { |
| 292 | $translations[$translationField][$lang] = wp_kses( |
| 293 | $translations[$translationField][$lang], |
| 294 | $allowedposttags |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | $data['translations'] = json_encode($translations); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param array $requestBody |
| 306 | * |
| 307 | * @return void |
| 308 | */ |
| 309 | protected function filter(&$requestBody) |
| 310 | { |
| 311 | if (!current_user_can('unfiltered_html') && $requestBody) { |
| 312 | $this->filterField($requestBody, 'description', 'description'); |
| 313 | $this->filterField($requestBody, 'label', 'name'); |
| 314 | |
| 315 | foreach (!empty($requestBody['extras']) ? $requestBody['extras'] : [] as $index => $extra) { |
| 316 | $this->filterField($requestBody['extras'][$index], 'description', 'description'); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 |