PHP Classes

File: apps/MS-AvailableHotels/app/HotelsProviders/Providers.php

Recommend this page to a friend!
  Classes of mohammad anzawi   PHP Hotel Booking Available   apps/MS-AvailableHotels/app/HotelsProviders/Providers.php   Download  
File: apps/MS-AvailableHotels/app/HotelsProviders/Providers.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Hotel Booking Available
Search for hotels that are available for booking
Author: By
Last change:
Date: 2 years ago
Size: 4,145 bytes
 

Contents

Class file image Download
<?php

namespace App\HotelsProviders;
use
Carbon\Carbon;
use
Faker\Factory as Faker;

/**
 * Hotels Providers Service
 */

/**
 * to add new provider
 * open (MS-AvailableHotels/config/providers.php) file & add new provider in array.
 * go to (MS-AvailableHotels/app/Transformer) directory and create new class with same provider name in config file
 * that's it.
 */
class Providers
{
    private
$_supportedProviders;

    public function
__construct()
    {
       
$this->_supportedProviders = config('providers');
    }

   
/**
     * Get all available hotels from providers
     * @param $provider
     * @param string $city
     * @param null $fromDate
     * @param null $toDate
     * @param int $numberOfAdults
     */
   
public function get($provider = null, $city = '', $fromDate = null, $toDate = null, $numberOfAdults = 1)
    {
       
$fromToDate = $this->formatDates($fromDate, $toDate);

        if (
$provider === null)
            return
$this->getFromAllProviders();

        return
$this->getFromProvider($provider);
    }

    private function
getFromAllProviders()
    {
       
$availableHotels = [];
       
// loop all supported providers and get available hotels
       
foreach ($this->_supportedProviders as $supportedProvider => $value) {
           
$availableHotels[] = $this->getFromProvider($supportedProvider);
        }

       
// convert multidimensional array to single array and then return results
       
return $this->margeAllHotels($availableHotels);
    }

    private function
getFromProvider($provider)
    {
       
// get provider url from config file
       
$providerUrl = config("providers.{$provider}");
       
// get the data
        // in this example we'll generate a fake date
       
$fakeData = [];
       
$faker = Faker::create();
       
$provider = ucwords($provider);
       
// init transformer
       
$transformer = app()->make('App\Transformers\\'.$provider . 'Transformer');

        for (
$i = 0; $i <= 10; $i++) {
           
$fakeData[$i] = [
               
'hotel' => $faker->firstName . ' Hotel',
               
'hotelRate' => $faker->randomNumber(1, 5),
               
'hotelFare' => $faker->randomFloat(2, 10, 150),
               
'roomAmenities' => ''
           
];

            foreach (
range($faker->randomNumber(1, 5), 5) as $index) {
               
$fakeData[$i]['roomAmenities'] .= $faker->emoji . ',';
            }

           
$fakeData[$i]['roomAmenities'] = rtrim($fakeData[$i]['roomAmenities'], ',');

           
// transform data to unify the data before response to front-end request.
           
$fakeData[$i] = $transformer->transform($fakeData[$i]);
        }

       
// order hotels by rate.
       
$rate = array_column($fakeData, 'rate');
       
array_multisort($rate, SORT_DESC, $fakeData);

        return
$fakeData;
    }

    public function
search($provider = null, $city = '', $fromDate = null, $toDate = null, $numberOfAdults = 1)
    {
       
/**
         * we don't have a real data to search in, so I'll return get() method!
         */
       
return $this->get($provider = null, $city = '', $fromDate = null, $toDate = null, $numberOfAdults = 1);
    }

   
/**
     * reformat dates, and set default from-to dates if not set
     * @param $fromDate
     * @param $toDate
     * @return array
     */
   
private function formatDates($fromDate, $toDate)
    {
        if(
$fromDate === null) $fromDate = Carbon::now();
        if(
$toDate === null) $toDate = Carbon::parse($fromDate)
            ->
addMonth();

        return [
           
'fromDate' => Carbon::parse($fromDate)->format('yyyy/MM/dd'),
           
'toDate' => Carbon::parse($toDate)->format('yyyy/MM/dd'),
        ];
    }

   
/**
     * @param $arr
     * @return array
     */
   
private function margeAllHotels($arr)
    {
       
$resutls = [];
        foreach (
$arr as $childArray)
        {
            foreach (
$childArray as $value)
            {
               
$resutls[] = $value;
            }
        }
       
$rate = array_column($resutls, 'rate');
       
array_multisort($rate, SORT_DESC, $resutls);

        return
$resutls;
    }
}