AnalyticsResponse::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 10
c 4
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 3
nc 3
nop 2
1
<?php
2
3
namespace TheIconic\Tracking\GoogleAnalytics;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Http\Promise\Promise;
8
9
/**
10
 * Class AnalyticsResponse
11
 *
12
 * Represents the response got from GA.
13
 *
14
 * @package TheIconic\Tracking\GoogleAnalytics
15
 */
16
class AnalyticsResponse implements AnalyticsResponseInterface
17
{
18
    /**
19
     * HTTP status code for the response.
20
     *
21
     * @var null|int
22
     */
23
    protected $httpStatusCode;
24
25
    /**
26
     * Request URI that was used to send the hit.
27
     *
28
     * @var string
29
     */
30
    protected $requestUrl;
31
32
    /**
33
     * Response body.
34
     *
35
     * @var string
36
     */
37
    protected $responseBody;
38
39
    /**
40
     * Gets the relevant data from the Guzzle clients.
41
     *
42
     * @param RequestInterface $request
43
     * @param ResponseInterface|Promise $response
44
     */
45
    public function __construct(RequestInterface $request, $response)
46
    {
47
        if ($response instanceof ResponseInterface) {
48
            $this->httpStatusCode = $response->getStatusCode();
49
            $this->responseBody = $response->getBody();
50
        } elseif ($response instanceof Promise) {
0 ignored issues
show
introduced by
$response is always a sub-type of Http\Promise\Promise.
Loading history...
51
            $this->httpStatusCode = null;
52
            $this->responseBody = null;
53
        } else {
54
            throw new \InvalidArgumentException(
55
                'Second constructor argument "response" must be instance of ResponseInterface or PromiseInterface'
56
            );
57
        }
58
59
        $this->requestUrl = (string) $request->getUri();
60
    }
61
62
    /**
63
     * Gets the HTTP status code.
64
     * It return NULL if the request was asynchronous since we are not waiting for the response.
65
     *
66
     * @api
67
     * @return null|int
68
     */
69
    public function getHttpStatusCode()
70
    {
71
        return $this->httpStatusCode;
72
    }
73
74
    /**
75
     * Gets the request URI used to get the response.
76
     *
77
     * @api
78
     * @return string
79
     */
80
    public function getRequestUrl()
81
    {
82
        return $this->requestUrl;
83
    }
84
85
    /**
86
     * Gets the debug response. Returns empty array if no response found.
87
     *
88
     * @api
89
     * @return array
90
     */
91
    public function getDebugResponse()
92
    {
93
        $debugResponse = [];
94
95
        if (!empty($this->responseBody)) {
96
            $debugResponse = json_decode($this->responseBody, true);
97
            $debugResponse = (is_array($debugResponse)) ? $debugResponse : [];
98
        }
99
100
        return $debugResponse;
101
    }
102
}