No Description

TransferStats.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace GuzzleHttp;
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. use Psr\Http\Message\UriInterface;
  6. /**
  7. * Represents data at the point after it was transferred either successfully
  8. * or after a network error.
  9. */
  10. final class TransferStats
  11. {
  12. private $request;
  13. private $response;
  14. private $transferTime;
  15. private $handlerStats;
  16. private $handlerErrorData;
  17. /**
  18. * @param RequestInterface $request Request that was sent.
  19. * @param ResponseInterface $response Response received (if any)
  20. * @param null $transferTime Total handler transfer time.
  21. * @param mixed $handlerErrorData Handler error data.
  22. * @param array $handlerStats Handler specific stats.
  23. */
  24. public function __construct(
  25. RequestInterface $request,
  26. ResponseInterface $response = null,
  27. $transferTime = null,
  28. $handlerErrorData = null,
  29. $handlerStats = []
  30. ) {
  31. $this->request = $request;
  32. $this->response = $response;
  33. $this->transferTime = $transferTime;
  34. $this->handlerErrorData = $handlerErrorData;
  35. $this->handlerStats = $handlerStats;
  36. }
  37. /**
  38. * @return RequestInterface
  39. */
  40. public function getRequest()
  41. {
  42. return $this->request;
  43. }
  44. /**
  45. * Returns the response that was received (if any).
  46. *
  47. * @return ResponseInterface|null
  48. */
  49. public function getResponse()
  50. {
  51. return $this->response;
  52. }
  53. /**
  54. * Returns true if a response was received.
  55. *
  56. * @return bool
  57. */
  58. public function hasResponse()
  59. {
  60. return $this->response !== null;
  61. }
  62. /**
  63. * Gets handler specific error data.
  64. *
  65. * This might be an exception, a integer representing an error code, or
  66. * anything else. Relying on this value assumes that you know what handler
  67. * you are using.
  68. *
  69. * @return mixed
  70. */
  71. public function getHandlerErrorData()
  72. {
  73. return $this->handlerErrorData;
  74. }
  75. /**
  76. * Get the effective URI the request was sent to.
  77. *
  78. * @return UriInterface
  79. */
  80. public function getEffectiveUri()
  81. {
  82. return $this->request->getUri();
  83. }
  84. /**
  85. * Get the estimated time the request was being transferred by the handler.
  86. *
  87. * @return float Time in seconds.
  88. */
  89. public function getTransferTime()
  90. {
  91. return $this->transferTime;
  92. }
  93. /**
  94. * Gets an array of all of the handler specific transfer data.
  95. *
  96. * @return array
  97. */
  98. public function getHandlerStats()
  99. {
  100. return $this->handlerStats;
  101. }
  102. /**
  103. * Get a specific handler statistic from the handler by name.
  104. *
  105. * @param string $stat Handler specific transfer stat to retrieve.
  106. *
  107. * @return mixed|null
  108. */
  109. public function getHandlerStat($stat)
  110. {
  111. return isset($this->handlerStats[$stat])
  112. ? $this->handlerStats[$stat]
  113. : null;
  114. }
  115. }