PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.19
Booking for Appointments and Events Calendar – Amelia v1.2.19
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 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Controller / Controller.php
ameliabooking / src / Application / Controller Last commit date
Bookable 2 years ago Booking 1 year ago Entities 4 years ago Import 4 years ago Notification 1 year ago Payment 2 years ago Settings 1 year ago Square 1 year ago Stash 4 years ago Stats 4 years ago Test 2 years ago User 1 year ago WhatsNew 2 years ago Controller.php 1 year ago
Controller.php
318 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 const STATUS_OK = 200;
27 const STATUS_REDIRECT = 302;
28 const STATUS_FORBIDDEN = 403;
29 const STATUS_NOT_FOUNT = 404;
30 const STATUS_CONFLICT = 409;
31 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
137 $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
138 $response = $response->withStatus(self::STATUS_INTERNAL_SERVER_ERROR);
139
140 $response = $response->write(json_encode([
141 'data' => [
142 'message' => $e->getMessage()
143 ]
144 ]));
145
146 return $response;
147 }
148
149 if ($commandResult->getResult() === CommandResult::RESULT_ERROR) {
150 if ($settingsService->getSetting('activation', 'responseErrorAsConflict')) {
151 $commandResult->setResult(CommandResult::RESULT_CONFLICT);
152 }
153 }
154
155 if ($commandResult->getUrl() !== null) {
156 $this->emitSuccessEvent($this->eventBus, $commandResult);
157
158 /** @var Response $response */
159 $response = $response->withHeader('Location', $commandResult->getUrl());
160 $response = $response->withStatus(self::STATUS_REDIRECT);
161
162 return $response;
163 }
164
165 if ($commandResult->hasAttachment() === false) {
166 $responseBody = [
167 'message' => $commandResult->getMessage(),
168 'data' => $commandResult->getData()
169 ];
170
171 $this->emitSuccessEvent($this->eventBus, $commandResult);
172
173 switch ($commandResult->getResult()) {
174 case (CommandResult::RESULT_SUCCESS):
175 $response = $response->withStatus(self::STATUS_OK);
176
177 break;
178 case (CommandResult::RESULT_CONFLICT):
179 $response = $response->withStatus(self::STATUS_CONFLICT);
180
181 break;
182 default:
183 $response = $response->withStatus(self::STATUS_INTERNAL_SERVER_ERROR);
184
185 break;
186 }
187
188 /** @var Response $response */
189 $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
190 $response = $response->write(
191 $this->sendJustData ? $commandResult->getData() :
192 json_encode(
193 $commandResult->hasDataInResponse() ?
194 $responseBody : array_merge($responseBody, ['data' => []])
195 )
196 );
197 }
198
199 if (($file = $commandResult->getFile()) !== null) {
200 /** @var Response $response */
201 $response = $response->withHeader('Content-Type', $file['type']);
202 $response = $response->withHeader('Content-Disposition', 'inline; filename=' . '"' . $file['name'] . '"');
203 $response = $response->withHeader('Cache-Control', 'max-age=0');
204
205 if (array_key_exists('size', $file)) {
206 $response = $response->withHeader('Content-Length', $file['size']);
207 }
208
209 $response = $response->write($file['content']);
210 }
211
212 return $response;
213 }
214
215 /**
216 * @param Command $command
217 * @param $requestBody
218 */
219 protected function setCommandFields($command, $requestBody)
220 {
221 foreach ($this->allowedFields as $field) {
222 if (!isset($requestBody[$field])) {
223 continue;
224 }
225 $command->setField($field, $requestBody[$field]);
226 }
227 }
228
229 /**
230 * @param mixed $params
231 */
232 protected function setArrayParams(&$params)
233 {
234 $names = [
235 'customers',
236 'categories',
237 'services',
238 'packages',
239 'employees',
240 'providers',
241 'providerIds',
242 'locations',
243 'events',
244 'dates',
245 'types',
246 'fields',
247 'statuses',
248 ];
249
250 foreach ($names as $name) {
251 if (!empty($params[$name])) {
252 $params[$name] = is_array($params[$name]) ? $params[$name] : explode(',', $params[$name]);
253 }
254 }
255
256 if (isset($params['dates'][0])) {
257 $params['dates'][0] = preg_match("/^\d{4}-\d{2}-\d{2}$/", $params['dates'][0]) ?
258 $params['dates'][0] : DateTimeService::getNowDate();
259 }
260
261 if (isset($params['dates'][1])) {
262 $params['dates'][1] = preg_match("/^\d{4}-\d{2}-\d{2}$/", $params['dates'][1]) ?
263 $params['dates'][1] : DateTimeService::getNowDate();
264 }
265
266 if (isset($params['date'])) {
267 $params['date'] = preg_match("/^\d{4}-\d{2}-\d{2}$/", $params['date']) ?
268 $params['date'] : DateTimeService::getNowDate();
269 }
270 }
271
272 /**
273 * @param array $data
274 * @param string $field
275 * @param string $translationField
276 *
277 * @return void
278 */
279 private function filterField(&$data, $field, $translationField)
280 {
281 if (!empty($data[$field])) {
282 global $allowedposttags;
283
284 $data[$field] = wp_kses($data[$field], $allowedposttags);
285
286 if (!empty($data['translations']) && ($translations = json_decode($data['translations'], true)) !== null) {
287 if (!empty($translations[$translationField])) {
288 foreach ($translations[$translationField] as $lang => $translation) {
289 $translations[$translationField][$lang] = wp_kses(
290 $translations[$translationField][$lang],
291 $allowedposttags
292 );
293 }
294
295 $data['translations'] = json_encode($translations);
296 }
297 }
298 }
299 }
300
301 /**
302 * @param array $requestBody
303 *
304 * @return void
305 */
306 protected function filter(&$requestBody)
307 {
308 if (!current_user_can('unfiltered_html') && $requestBody) {
309 $this->filterField($requestBody, 'description', 'description');
310 $this->filterField($requestBody, 'label', 'name');
311
312 foreach (!empty($requestBody['extras']) ? $requestBody['extras'] : [] as $index => $extra) {
313 $this->filterField($requestBody['extras'][$index], 'description', 'description');
314 }
315 }
316 }
317 }
318