Bookable
1 year ago
Booking
1 year ago
Entities
1 year ago
Import
1 year ago
Notification
1 year ago
Payment
1 year ago
Settings
1 year ago
Square
1 year ago
Stash
1 year ago
Stats
1 year ago
Test
1 year ago
User
1 year ago
WhatsNew
1 year ago
Command.php
1 year ago
CommandHandler.php
1 year ago
CommandResult.php
1 year ago
Command.php
247 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\Booking\Appointment\AddBookingCommand; |
| 6 | use AmeliaBooking\Application\Commands\Booking\Appointment\SuccessfulBookingCommand; |
| 7 | use AmeliaBooking\Application\Commands\Notification\GetSMSNotificationsHistoryCommand; |
| 8 | use AmeliaBooking\Application\Commands\Payment\CalculatePaymentAmountCommand; |
| 9 | use AmeliaBooking\Application\Commands\Square\DisconnectFromSquareAccountCommand; |
| 10 | use AmeliaBooking\Application\Commands\Square\FetchAccessTokenSquareCommand; |
| 11 | use AmeliaBooking\Application\Commands\Square\SquareRefundWebhookCommand; |
| 12 | use AmeliaBooking\Application\Commands\Square\SquarePaymentCommand; |
| 13 | use AmeliaBooking\Application\Commands\Stats\AddStatsCommand; |
| 14 | use AmeliaBooking\Application\Services\User\UserApplicationService; |
| 15 | use AmeliaBooking\Domain\Services\Permissions\PermissionsService; |
| 16 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 17 | use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage; |
| 18 | use Slim\Http\Request; |
| 19 | |
| 20 | /** |
| 21 | * Class Command |
| 22 | * |
| 23 | * @package AmeliaBooking\Application\Commands |
| 24 | */ |
| 25 | abstract class Command |
| 26 | { |
| 27 | protected $args; |
| 28 | |
| 29 | protected $container; |
| 30 | |
| 31 | private $fields = []; |
| 32 | |
| 33 | public $token; |
| 34 | |
| 35 | private $page; |
| 36 | |
| 37 | private $cabinetType; |
| 38 | |
| 39 | private $permissionService; |
| 40 | |
| 41 | private $userApplicationService; |
| 42 | |
| 43 | /** |
| 44 | * Command constructor. |
| 45 | * |
| 46 | * @param $args |
| 47 | */ |
| 48 | public function __construct($args) |
| 49 | { |
| 50 | $this->args = $args; |
| 51 | if (isset($args['type'])) { |
| 52 | $this->setField('type', $args['type']); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return mixed |
| 58 | */ |
| 59 | public function getArgs() |
| 60 | { |
| 61 | return $this->args; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param mixed $arg Argument to be fetched |
| 66 | * |
| 67 | * @return null|mixed |
| 68 | */ |
| 69 | public function getArg($arg) |
| 70 | { |
| 71 | return isset($this->args[$arg]) ? $this->args[$arg] : null; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param $fieldName |
| 76 | * @param $fieldValue |
| 77 | */ |
| 78 | public function setField($fieldName, $fieldValue) |
| 79 | { |
| 80 | $this->fields[$fieldName] = $fieldValue; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param $fieldName |
| 85 | */ |
| 86 | public function removeField($fieldName) |
| 87 | { |
| 88 | unset($this->fields[$fieldName]); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return a single field |
| 93 | * |
| 94 | * @param $fieldName |
| 95 | * |
| 96 | * @return mixed|null |
| 97 | */ |
| 98 | public function getField($fieldName) |
| 99 | { |
| 100 | return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return all fields |
| 105 | * |
| 106 | * @return array |
| 107 | */ |
| 108 | public function getFields() |
| 109 | { |
| 110 | return $this->fields; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Set Token |
| 115 | * |
| 116 | * @param Request $request |
| 117 | */ |
| 118 | public function setToken($request) |
| 119 | { |
| 120 | $headers = $request->getHeaders(); |
| 121 | |
| 122 | $token = null; |
| 123 | |
| 124 | /** @var SettingsService $settingsService */ |
| 125 | $settingsService = new SettingsService(new SettingsStorage()); |
| 126 | |
| 127 | if (isset($headers['HTTP_AUTHORIZATION'][0]) && |
| 128 | ($values = explode(' ', $request->getHeaders()['HTTP_AUTHORIZATION'][0])) && |
| 129 | sizeof($values) === 2 && |
| 130 | $settingsService->getSetting('roles', 'enabledHttpAuthorization') |
| 131 | ) { |
| 132 | $token = $values[1]; |
| 133 | } else if (isset($headers['HTTP_COOKIE'][0])) { |
| 134 | foreach (explode('; ', $headers['HTTP_COOKIE'][0]) as $cookie) { |
| 135 | if (($ameliaTokenCookie = explode('=', $cookie)) && $ameliaTokenCookie[0] === 'ameliaToken') { |
| 136 | $token = $ameliaTokenCookie[1]; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $this->token = $token; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Return Token |
| 146 | * |
| 147 | * @return string|null |
| 148 | */ |
| 149 | public function getToken() |
| 150 | { |
| 151 | return $this->token; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Set page |
| 156 | * |
| 157 | * @param string $page |
| 158 | */ |
| 159 | public function setPage($page) |
| 160 | { |
| 161 | $this->page = explode('-', $page)[0]; |
| 162 | |
| 163 | $this->cabinetType = !empty(explode('-', $page)[1]) ? explode('-', $page)[1] : null; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Return page |
| 168 | * |
| 169 | * @return string|null |
| 170 | */ |
| 171 | public function getPage() |
| 172 | { |
| 173 | return $this->page; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @param $request |
| 178 | * @return string|null |
| 179 | */ |
| 180 | public function validateNonce($request) |
| 181 | { |
| 182 | if ($request->getMethod() === 'POST' && |
| 183 | !self::getToken() && |
| 184 | !($this instanceof CalculatePaymentAmountCommand) && |
| 185 | !($this instanceof AddBookingCommand) && |
| 186 | !($this instanceof AddStatsCommand) && |
| 187 | !($this instanceof SquarePaymentCommand) && |
| 188 | !($this instanceof SquareRefundWebhookCommand) && |
| 189 | !($this instanceof DisconnectFromSquareAccountCommand) && |
| 190 | !($this instanceof SuccessfulBookingCommand) && |
| 191 | !($this instanceof FetchAccessTokenSquareCommand) && |
| 192 | !($this instanceof GetSMSNotificationsHistoryCommand) |
| 193 | ) { |
| 194 | $queryParams = $request->getQueryParams(); |
| 195 | |
| 196 | return wp_verify_nonce( |
| 197 | !empty($queryParams['wpAmeliaNonce']) ? $queryParams['wpAmeliaNonce'] : $queryParams['ameliaNonce'], |
| 198 | 'ajax-nonce' |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Return cabinet type |
| 207 | * |
| 208 | * @return string|null |
| 209 | */ |
| 210 | public function getCabinetType() |
| 211 | { |
| 212 | return $this->cabinetType; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @return PermissionsService |
| 217 | */ |
| 218 | public function getPermissionService() |
| 219 | { |
| 220 | return $this->permissionService; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @param PermissionsService $permissionService |
| 225 | */ |
| 226 | public function setPermissionService($permissionService) |
| 227 | { |
| 228 | $this->permissionService = $permissionService; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @return UserApplicationService |
| 233 | */ |
| 234 | public function getUserApplicationService() |
| 235 | { |
| 236 | return $this->userApplicationService; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @param UserApplicationService $userApplicationService |
| 241 | */ |
| 242 | public function setUserApplicationService($userApplicationService) |
| 243 | { |
| 244 | $this->userApplicationService = $userApplicationService; |
| 245 | } |
| 246 | } |
| 247 |