TimeMachine.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. class ChargeBee_TimeMachine extends ChargeBee_Model
  3. {
  4. protected $allowed = array('name', 'timeTravelStatus', 'genesisTime', 'destinationTime', 'failureCode', 'failureReason',
  5. 'errorJson');
  6. public function waitForTimeTravelCompletion($env = null) {
  7. $count = 0;
  8. $tm = $this;
  9. while($this->timeTravelStatus == "in_progress") {
  10. if($count++ > 30){
  11. throw new RuntimeException("The time travel is taking too much time");
  12. }
  13. sleep(ChargeBee_Environment::$timeMachineWaitInSecs);
  14. $this->_values = ChargeBee_TimeMachine::retrieve($this->name,$env)->timeMachine()->getValues();
  15. $this->_load();
  16. }
  17. if($this->timeTravelStatus == "failed" ) {
  18. $errorJSON = json_decode($this->errorJson, true);
  19. $httpCode = $errorJSON['http_code'];
  20. throw new ChargeBee_OperationFailedException($httpCode, $errorJSON);
  21. }
  22. if($this->timeTravelStatus != "in_progress"
  23. && $this->timeTravelStatus != "succeeded"
  24. && $this->timeTravelStatus != "failed" ) {
  25. throw new RuntimeException("Time travel status is in wrong state " . $this->timeTravelStatus);
  26. }
  27. return $this;
  28. }
  29. # OPERATIONS
  30. #-----------
  31. public static function retrieve($id, $env = null, $headers = array())
  32. {
  33. return ChargeBee_Request::send(ChargeBee_Request::GET, ChargeBee_Util::encodeURIPath("time_machines",$id), array(), $env, $headers);
  34. }
  35. public static function startAfresh($id, $params = array(), $env = null, $headers = array())
  36. {
  37. return ChargeBee_Request::send(ChargeBee_Request::POST, ChargeBee_Util::encodeURIPath("time_machines",$id,"start_afresh"), $params, $env, $headers);
  38. }
  39. public static function travelForward($id, $params = array(), $env = null, $headers = array())
  40. {
  41. return ChargeBee_Request::send(ChargeBee_Request::POST, ChargeBee_Util::encodeURIPath("time_machines",$id,"travel_forward"), $params, $env, $headers);
  42. }
  43. }
  44. ?>