PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.8
Booking for Appointments and Events Calendar – Amelia v2.4.8
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 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 / Commands / CommandHandler.php
ameliabooking / src / Application / Commands Last commit date
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