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