Advertisement
miedzwin

Untitled

Jul 25th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.32 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. final class BuildBookingFlagCommandHandler implements CommandHandler
  6. {
  7.     private const MAX_RETRY_COUNT = 3;
  8.  
  9.     public function __construct(
  10.         private CommandBus $commandBus,
  11.         private QueryBus $queryBus,
  12.         private AccommodationFlagBuilder $accommodationFlagBuilder,
  13.         private LandServicesFlagBuilder $landServicesFlagBuilder,
  14.         private FlightFlagsBuilder $flightFlagsBuilder,
  15.         private NotesFlagBuilder $notesFlagBuilder,
  16.         private PassportFlagBuilder $passportFlagBuilder,
  17.         private PaymentFlagBuilder $paymentFlagBuilder,
  18.     ) {
  19.     }
  20.  
  21.     public function __invoke(BuildBookingFlagCommand $command): void
  22.     {
  23.         $accommodationFlag = $this->accommodationFlagBuilder->build($command->bookingId());
  24.         $landServicesFlag = $this->landServicesFlagBuilder->build($command->bookingId());
  25.         $flightFlags = $this->flightFlagsBuilder->build($command->bookingId());
  26.         $notesFlag = $this->notesFlagBuilder->build($command->bookingId());
  27.         $passportFlag = $this->passportFlagBuilder->build($command->bookingId());
  28.         $paymentFlag = $this->paymentFlagBuilder->build($command->bookingId());
  29.  
  30.         $this->processFlagUpdate(
  31.             bookingId: $command->bookingId(),
  32.             landServices: $landServicesFlag->value(),
  33.             flightServices: $flightFlags->servicesFlag()->value(),
  34.             accommodations: $accommodationFlag->value(),
  35.             notes: $notesFlag->value(),
  36.             passport: $passportFlag->value(),
  37.             payments: $paymentFlag->value(),
  38.             flightLuggage: $flightFlags->luggageFlag()->value(),
  39.             flightAirport: $flightFlags->airportFlag()->value(),
  40.             flightTravelfusion: $flightFlags->travelfusionFlag()->value(),
  41.             flightAccommodationDates: $flightFlags->accommodationDatesFlag()->value(),
  42.             flightLayover: $flightFlags->layoverFlag()->value(),
  43.             updatedBy: $command->createdBy(),
  44.             retryCount: 1,
  45.         );
  46.     }
  47.  
  48.     private function processFlagUpdate(
  49.         int $bookingId,
  50.         int $landServices,
  51.         int $flightServices,
  52.         int $accommodations,
  53.         int $notes,
  54.         int $passport,
  55.         int $payments,
  56.         int $flightLuggage,
  57.         int $flightAirport,
  58.         int $flightTravelfusion,
  59.         int $flightAccommodationDates,
  60.         int $flightLayover,
  61.         string $updatedBy,
  62.         int $retryCount,
  63.     ): void {
  64.         if ($retryCount >= self::MAX_RETRY_COUNT) {
  65.             throw new TooManyRetriesDuringBookingFlagGeneration(BookingId::of($bookingId));
  66.         }
  67.  
  68.         try {
  69.             $this->queryBus->ask(
  70.                 new FindBookingFlagQuery($bookingId)
  71.             );
  72.             $this->commandBus->dispatch(
  73.                 new UpdateBookingFlagCommand(
  74.                     bookingId: $bookingId,
  75.                     landServices: $landServices,
  76.                     flightServices: $flightServices,
  77.                     accommodations: $accommodations,
  78.                     notes: $notes,
  79.                     passport: $passport,
  80.                     payments: $payments,
  81.                     flightLuggage: $flightLuggage,
  82.                     flightAirport: $flightAirport,
  83.                     flightTravelfusion: $flightTravelfusion,
  84.                     flightAccommodationDates: $flightAccommodationDates,
  85.                     flightLayover: $flightLayover,
  86.                     updatedBy: $updatedBy,
  87.                 )
  88.             );
  89.         } catch (BookingFlagNotFound) {
  90.             try {
  91.                 $this->commandBus->dispatch(
  92.                     new CreateBookingFlagCommand(
  93.                         bookingId: $bookingId,
  94.                         landServices: $landServices,
  95.                         flightServices: $flightServices,
  96.                         accommodations: $accommodations,
  97.                         notes: $notes,
  98.                         salesforce: Flag::LEVEL_GREEN,
  99.                         passport: $passport,
  100.                         payments: $payments,
  101.                         flightLuggage: $flightLuggage,
  102.                         flightAirport: $flightAirport,
  103.                         flightTravelfusion: $flightTravelfusion,
  104.                         flightAccommodationDates: $flightAccommodationDates,
  105.                         flightLayover: $flightLayover,
  106.                         createdBy: $updatedBy,
  107.                     )
  108.                 );
  109.             } catch (BookingFlagAlreadyCreated) {
  110.                 usleep(25_000);
  111.                 $this->processFlagUpdate(
  112.                     bookingId: $bookingId,
  113.                     landServices: $landServices,
  114.                     flightServices: $flightServices,
  115.                     accommodations: $accommodations,
  116.                     notes: $notes,
  117.                     passport: $passport,
  118.                     payments: $payments,
  119.                     flightLuggage: $flightLuggage,
  120.                     flightAirport: $flightAirport,
  121.                     flightTravelfusion: $flightTravelfusion,
  122.                     flightAccommodationDates: $flightAccommodationDates,
  123.                     flightLayover: $flightLayover,
  124.                     updatedBy: $updatedBy,
  125.                     retryCount: ++$retryCount
  126.                 );
  127.             }
  128.         }
  129.     }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement