Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- final class BuildBookingFlagCommandHandler implements CommandHandler
- {
- private const MAX_RETRY_COUNT = 3;
- public function __construct(
- private CommandBus $commandBus,
- private QueryBus $queryBus,
- private AccommodationFlagBuilder $accommodationFlagBuilder,
- private LandServicesFlagBuilder $landServicesFlagBuilder,
- private FlightFlagsBuilder $flightFlagsBuilder,
- private NotesFlagBuilder $notesFlagBuilder,
- private PassportFlagBuilder $passportFlagBuilder,
- private PaymentFlagBuilder $paymentFlagBuilder,
- ) {
- }
- public function __invoke(BuildBookingFlagCommand $command): void
- {
- $accommodationFlag = $this->accommodationFlagBuilder->build($command->bookingId());
- $landServicesFlag = $this->landServicesFlagBuilder->build($command->bookingId());
- $flightFlags = $this->flightFlagsBuilder->build($command->bookingId());
- $notesFlag = $this->notesFlagBuilder->build($command->bookingId());
- $passportFlag = $this->passportFlagBuilder->build($command->bookingId());
- $paymentFlag = $this->paymentFlagBuilder->build($command->bookingId());
- $this->processFlagUpdate(
- bookingId: $command->bookingId(),
- landServices: $landServicesFlag->value(),
- flightServices: $flightFlags->servicesFlag()->value(),
- accommodations: $accommodationFlag->value(),
- notes: $notesFlag->value(),
- passport: $passportFlag->value(),
- payments: $paymentFlag->value(),
- flightLuggage: $flightFlags->luggageFlag()->value(),
- flightAirport: $flightFlags->airportFlag()->value(),
- flightTravelfusion: $flightFlags->travelfusionFlag()->value(),
- flightAccommodationDates: $flightFlags->accommodationDatesFlag()->value(),
- flightLayover: $flightFlags->layoverFlag()->value(),
- updatedBy: $command->createdBy(),
- retryCount: 1,
- );
- }
- private function processFlagUpdate(
- int $bookingId,
- int $landServices,
- int $flightServices,
- int $accommodations,
- int $notes,
- int $passport,
- int $payments,
- int $flightLuggage,
- int $flightAirport,
- int $flightTravelfusion,
- int $flightAccommodationDates,
- int $flightLayover,
- string $updatedBy,
- int $retryCount,
- ): void {
- if ($retryCount >= self::MAX_RETRY_COUNT) {
- throw new TooManyRetriesDuringBookingFlagGeneration(BookingId::of($bookingId));
- }
- try {
- $this->queryBus->ask(
- new FindBookingFlagQuery($bookingId)
- );
- $this->commandBus->dispatch(
- new UpdateBookingFlagCommand(
- bookingId: $bookingId,
- landServices: $landServices,
- flightServices: $flightServices,
- accommodations: $accommodations,
- notes: $notes,
- passport: $passport,
- payments: $payments,
- flightLuggage: $flightLuggage,
- flightAirport: $flightAirport,
- flightTravelfusion: $flightTravelfusion,
- flightAccommodationDates: $flightAccommodationDates,
- flightLayover: $flightLayover,
- updatedBy: $updatedBy,
- )
- );
- } catch (BookingFlagNotFound) {
- try {
- $this->commandBus->dispatch(
- new CreateBookingFlagCommand(
- bookingId: $bookingId,
- landServices: $landServices,
- flightServices: $flightServices,
- accommodations: $accommodations,
- notes: $notes,
- salesforce: Flag::LEVEL_GREEN,
- passport: $passport,
- payments: $payments,
- flightLuggage: $flightLuggage,
- flightAirport: $flightAirport,
- flightTravelfusion: $flightTravelfusion,
- flightAccommodationDates: $flightAccommodationDates,
- flightLayover: $flightLayover,
- createdBy: $updatedBy,
- )
- );
- } catch (BookingFlagAlreadyCreated) {
- usleep(25_000);
- $this->processFlagUpdate(
- bookingId: $bookingId,
- landServices: $landServices,
- flightServices: $flightServices,
- accommodations: $accommodations,
- notes: $notes,
- passport: $passport,
- payments: $payments,
- flightLuggage: $flightLuggage,
- flightAirport: $flightAirport,
- flightTravelfusion: $flightTravelfusion,
- flightAccommodationDates: $flightAccommodationDates,
- flightLayover: $flightLayover,
- updatedBy: $updatedBy,
- retryCount: ++$retryCount
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement