Bookable
1 month ago
Booking
1 week ago
Calendar
2 weeks ago
Entities
2 weeks ago
Google
5 months ago
Import
1 week ago
Mobile
2 months ago
Notification
2 months ago
Payment
1 week ago
QrCode
2 weeks ago
Settings
1 week ago
Square
1 week ago
Stash
2 weeks ago
Stats
8 months ago
Test
8 months ago
User
1 week ago
WhatsNew
5 months ago
Command.php
1 week ago
CommandHandler.php
1 week ago
CommandResult.php
6 months ago
SortParamsTrait.php
5 months ago
CommandHandler.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands; |
| 4 | |
| 5 | use AmeliaBooking\Application\Services\Booking\BookingApplicationService; |
| 6 | use AmeliaBooking\Application\Services\Payment\PaymentApplicationService; |
| 7 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\PaymentValidationException; |
| 9 | use AmeliaBooking\Domain\ValueObjects\String\PaymentType; |
| 10 | use AmeliaBooking\Infrastructure\Common\Container; |
| 11 | use Exception; |
| 12 | |
| 13 | /** |
| 14 | * Class CommandHandler |
| 15 | * |
| 16 | * @package AmeliaBooking\Application\Commands |
| 17 | */ |
| 18 | abstract class CommandHandler |
| 19 | { |
| 20 | /** |
| 21 | * @var Container |
| 22 | */ |
| 23 | protected $container; |
| 24 | |
| 25 | protected $mandatoryFields = []; |
| 26 | |
| 27 | /** |
| 28 | * @param Command $command |
| 29 | * |
| 30 | * @return void |
| 31 | * @throws InvalidArgumentException |
| 32 | */ |
| 33 | public function checkMandatoryFields($command): void |
| 34 | { |
| 35 | $missingFields = []; |
| 36 | |
| 37 | foreach ($this->mandatoryFields as $field) { |
| 38 | if ($command->getField($field) === null) { |
| 39 | $missingFields[] = $field; |
| 40 | } |
| 41 | } |
| 42 | if (!empty($missingFields)) { |
| 43 | throw new InvalidArgumentException( |
| 44 | 'Mandatory fields not passed! Missing: ' . implode(', ', $missingFields) |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Normalize a booking request payload and replace its payment object with the |
| 51 | * server-derived equivalent processBooking / processRequest expect. |
| 52 | * |
| 53 | * Call this after checkMandatoryFields on handlers that receive a booking request. |
| 54 | * Pass the payload to normalize - usually $command->getFields(), or a cached/mutated copy. |
| 55 | * |
| 56 | * @param array $data |
| 57 | * @param array|null $allowedGateways Gateways this endpoint accepts, defaults to null (skip payment data validation) |
| 58 | * |
| 59 | * @return array |
| 60 | * @throws PaymentValidationException |
| 61 | * @throws Exception |
| 62 | */ |
| 63 | protected function getAppointmentData(array $data, $allowedGateways = null): array |
| 64 | { |
| 65 | /** @var BookingApplicationService $bookingAS */ |
| 66 | $bookingAS = $this->container->get('application.booking.booking.service'); |
| 67 | |
| 68 | $data = $bookingAS->getAppointmentData($data); |
| 69 | |
| 70 | // if no payment data is provided, use the default on-site payment |
| 71 | // if call is ment to check payment data, it will be validated later |
| 72 | if (!isset($data['payment'])) { |
| 73 | $data['payment'] = ['gateway' => PaymentType::ON_SITE]; |
| 74 | } |
| 75 | |
| 76 | // skip validation if no allowed gateways are provided |
| 77 | if ($allowedGateways === null) { |
| 78 | return $data; |
| 79 | } |
| 80 | |
| 81 | /** @var PaymentApplicationService $paymentAS */ |
| 82 | $paymentAS = $this->container->get('application.payment.service'); |
| 83 | |
| 84 | $paymentAS->validateRequestPaymentData($data, $allowedGateways); |
| 85 | |
| 86 | return $data; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * CommandHandler constructor. |
| 91 | * |
| 92 | * @param Container $container |
| 93 | */ |
| 94 | public function __construct($container) |
| 95 | { |
| 96 | $this->container = $container; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return Container |
| 101 | */ |
| 102 | public function getContainer(): Container |
| 103 | { |
| 104 | return $this->container; |
| 105 | } |
| 106 | } |
| 107 |