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