TransferStats.php 3.1 KB

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