Advertisement
elmika

Fake Booking Object

Apr 9th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. <?php
  2.  
  3. namespace BookingNamespace;
  4. /**
  5. * Simulate expiration process with two public methods:
  6. *      - book will return a token
  7. *      - confirm will take a token, and return
  8. *           -> true if called within 5 minutes of corresponding book call
  9. *           -> false if more than 5 minutes have passed
  10. */
  11. class MyBookingObject
  12. {
  13.     private $tokens;            // array with tokens as keys, dates as values
  14.     private $timeout_delay;     // seconds
  15.  
  16.     /**
  17.     *   Empty tokens, 5 minutes timeout
  18.     */
  19.     public function __construct()
  20.     {
  21.         $this->tokens = array();
  22.         $this->timeout_delay = 5 * 60;
  23.     }
  24.  
  25.     private function generate_random_string(){ return substr(md5(rand()), 0, 7); }
  26.     private function has_token($token){ return isset($this->tokens[$token]); }
  27.     private function init_token($token){ $this->tokens[$token] = new DateTime(); }
  28.     private function get_booking_date($token){ return $this->tokens[$token]; }
  29.     private function is_over_timeout($seconds){ return $seconds > $this->timeout_delay; }
  30.  
  31.     /**
  32.      *  Our booking time has timed out if it has happened too long ago
  33.      *  @return true if $booking_time is too long ago
  34.      *          false if we are on time
  35.      */
  36.     private function has_timed_out(DateTime $booking_time)
  37.     {
  38.         $now = new DateTime();
  39.         if($booking_time > $now) throw new Exception("Booking time should be in the past.");
  40.         $diff = $now->getTimestamp() - $booking_time->getTimestamp();
  41.  
  42.         return $this->is_over_timeout($diff);
  43.     }
  44.  
  45.     /**
  46.      * @throws Exception if there is more bookings than the system can handle
  47.      * @return string: the token for our booking.
  48.      */
  49.     public function book()
  50.     {
  51.         if(count($this->tokens) > 10000) throw new Exception("That's too many bookings");
  52.         do {
  53.             $token = $this->generate_random_string();          
  54.         }
  55.         while($this->has_token($token));
  56.  
  57.         $this->init_token($token); 
  58.         return $token;
  59.     }
  60.  
  61.     /**
  62.      * @param string $token: the booking token
  63.      * @throws Exception if this token cannot be found
  64.      * @return boolean true if confirmation ok
  65.      *                 false if confirmation timed out
  66.      */
  67.     public function confirm($token)
  68.     {
  69.         if( ! $this->has_token($token) ) throw new Exception("Incorrect token for confirmation");
  70.         $booking_date = $this->get_booking_date($token);
  71.        
  72.         if($this->has_timed_out($booking_date)) return false;
  73.  
  74.         return true;
  75.     }
  76.  
  77. }
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement