TextClientResult.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace CMText;
  3. /**
  4. * Class TextClientResult
  5. *
  6. * @package CMText
  7. */
  8. class TextClientResult
  9. {
  10. /**
  11. * @var int
  12. */
  13. private $httpStatusCode;
  14. /**
  15. * @var string
  16. */
  17. private $response;
  18. /**
  19. * TextClientResult constructor.
  20. *
  21. * @param int $httpStatusCode
  22. * @param string $responseBody
  23. */
  24. public function __construct(int $httpStatusCode, $responseBody = '')
  25. {
  26. $this->httpStatusCode = $httpStatusCode;
  27. $this->response = $responseBody;
  28. $this->processResponse();
  29. }
  30. /**
  31. * Processes the Response from the gateway into a TextClientResult model.
  32. */
  33. private function processResponse()
  34. {
  35. // decode the response
  36. $json = json_decode($this->response, false, 5);
  37. if(null === $json){
  38. $this->statusMessage = strlen($this->response) ? substr($this->response, 0, 100) : 'An error occurred';
  39. $this->statusCode = TextClientStatusCodes::UNKNOWN;
  40. }else{
  41. $this->statusMessage = $json->details ?? 'An error occurred';
  42. $this->statusCode = $json->errorCode ?? TextClientStatusCodes::UNKNOWN;
  43. $this->details = $json->messages ?? [];
  44. }
  45. }
  46. /**
  47. * @var string
  48. */
  49. public $statusMessage = '';
  50. /**
  51. * @var int
  52. */
  53. public $statusCode = 0;
  54. /**
  55. * @var array
  56. */
  57. public $details = [];
  58. }