SlackResponse::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php namespace Slacky\Http;
2
3
use Slacky\Contracts\Http\Response;
4
5
/**
6
 * Class SlackResponse
7
 * @package Frlnc\Slack\Http
8
 */
9
class SlackResponse implements Response, \JsonSerializable {
10
11
    /**
12
     * The response body.
13
     *
14
     * @var string
15
     */
16
    protected $body;
17
18
    /**
19
     * The response headers.
20
     *
21
     * @var array
22
     */
23
    protected $headers;
24
25
    /**
26
     * The response status code.
27
     *
28
     * @var integer
29
     */
30
    protected $statusCode;
31
32
    /**
33
     * @param string  $body
34
     * @param array   $headers
35
     * @param integer $statusCode
36
     */
37
    public function __construct($body, array $headers = [], $statusCode = 404)
38
    {
39
        $this->body = json_decode($body, true);
40
        $this->headers = $headers;
41
        $this->statusCode = $statusCode;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getBody(): array
48
    {
49
        return $this->body;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->body returns the type string which is incompatible with the type-hinted return array.
Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getHeaders(): array
56
    {
57
        return $this->headers;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getStatusCode(): int
64
    {
65
        return $this->statusCode;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function jsonSerialize()
72
    {
73
        return $this->toArray();
74
    }
75
76
    /**
77
     * Converts the response to an array.
78
     *
79
     * @return array
80
     */
81
    public function toArray() : array
82
    {
83
        return [
84
            'status_code' => $this->getStatusCode(),
85
            'headers'     => $this->getHeaders(),
86
            'body'        => $this->getBody()
87
        ];
88
    }
89
90
}
91