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