jeudi 23 mai 2019

Is this correct usage of Builder pattern

I have a scenario like this:

The system handles reservations from couple of different sources. All bookings come in the form of a json with differing formats for each source. Some JSON attributes are present in one JSON and not in the other etc.

In order to enter the reservation into the system, I am trying to centralize the Add Reservation logic into one method. To do this, I am trying to create a class called Reservation using the Builder pattern as such:

<?php

namespace App\Business\Classes\Reservation;


use App\Business\Classes\Utils\ReservationUtils;

/**
 * Class Reservation
 * Holds entire reservation details
 * @package App\Business\Classes\Reservation
*/
class Reservation
{
    protected $reference, $otaReference, $hotelCode, $customerId, $guestName, $couponId, $checkin, $checkout, $guests, $customerCountry,
    $commissionRate, $paymentStatus, $commissionOta, $commissionDifference, $rooms = [], $cityTax, $vat, $serviceCharge, $geniusBooker = false,
    $geniusAmount = 0, $taxes, $taxIncluded = false, $supplierAmount, $totalAmount, $specialRequest, $currency, $deviceType, $bookingSource,
    $otaRefId, $status, $source, $remarks;

    public function __construct()
    {
        // generates a reference
        $this->reference = ReservationUtils::referenceGenerator();
    }

     /**
      * @param mixed $otaReference
      * @return Reservation
     */
     public function setOtaReference($otaReference): Reservation
     {
        $this->otaReference = $otaReference;
        return $this;
     }

     /**
      * @param mixed $checkin
      * @return Reservation
     */
     public function setCheckin($checkin): Reservation
     {
        $this->checkin = $checkin;
        return $this;
     }

     /**
      * @param mixed $checkout
      * @return Reservation
     */
     public function setCheckout($checkout): Reservation
     {
        $this->checkout = $checkout;
        return $this;
     }

     /**
      * @param array $rooms
      * @return Reservation
     */
     public function setRooms(array $rooms): Reservation
     {
        $this->rooms = $rooms;
        return $this;
     }
     // and so on....
}

And I would use it like this:

$data= json_decode($result->jsonData);
$reservation = (new Reservation())
                  ->setOtaReference($data->otaReference)
                  ->setCheckin($data->checkin)
                  ->setCheckout($data->checkout); //...etc

This is probably not the textbook usage of the builder pattern in PHP or even Java but is there anything wrong with it?

Thanks

Aucun commentaire:

Enregistrer un commentaire