Completed
Push — master ( ae5671...553781 )
by Paul
01:16
created

Response::getTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Infinitypaul\NairaExchangeRates;
5
6
7
class Response
8
{
9
    # The actual Guzzle response:
10
    private $response;
11
12
    # Core response:
13
    private $headers;
14
    private $bodyRaw;
15
    private $body;
16
17
    # Properties:
18
    private $statusCode;
19
    private $timestamp;
20
21
    function __construct( \GuzzleHttp\Psr7\Response $response = null )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        $this->response = $response;
24
        $this->headers    = $response->getHeaders();
0 ignored issues
show
Bug introduced by
It seems like $response is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
25
        $this->bodyRaw    = (string) $response->getBody();
26
        $this->body       = json_decode( $this->bodyRaw );
27
28
        # Set our properties:
29
        $this->statusCode   = $response->getStatusCode();
30
        $this->timestamp    = date('c');
31
    }
32
33
    /****************************/
34
    /*                          */
35
    /*         GETTERS          */
36
    /*                          */
37
    /****************************/
38
39
    # Get the status code:
40
    public function getStatusCode()
41
    {
42
        return (int) $this->statusCode;
43
    }
44
45
    #ÊGet the timestamp of the request:
46
    public function getTimestamp()
47
    {
48
        return $this->timestamp;
49
    }
50
51
    #ÊGet the timestamp of the request:
52
    public function getData()
53
    {
54
        return $this->body;
55
    }
56
57
    #ÊConvert the response to JSON:
58
    public function toJSON()
59
    {
60
        return json_encode([
61
            'statusCode'   => $this->getStatusCode(),
62
            'timestamp'    => $this->getTimestamp(),
63
            'body' => $this->getData()
64
        ]);
65
    }
66
}
67