AbstractApiGatewayResponse::addHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Guillermoandrae\Lambda\Contracts;
4
5
use Guillermoandrae\Http\StatusCodes;
6
7
abstract class AbstractApiGatewayResponse implements ApiGatewayResponseInterface
8
{
9
    /**
10
     * The event data from the API Gateway request.
11
     *
12
     * @var array
13
     */
14
    protected array $event;
15
16
    /**
17
     * HTTP status code; OK returned by default.
18
     *
19
     * @var int
20
     */
21
    protected int $statusCode = StatusCodes::OK;
22
23
    /**
24
     * An array of headers to be sent with the response.
25
     *
26
     * @var array
27
     */
28
    protected array $headers = [];
29
30
    /**
31
     * An array of headers to be sent with every response.
32
     *
33
     * @var array
34
     */
35
    protected array $requiredHeaders = [
36
        'Access-Control-Allow-Origin' => '*',
37
    ];
38
39
    /**
40
     * An array of data that represents the body of the response.
41
     *
42
     * @var array
43
     */
44
    protected array $body = [];
45
46
    final public function setEvent(array $event): static
47 1
    {
48
        $this->event = $event;
49 1
        return $this;
50 1
    }
51
52
    final public function getEvent(): array
53 1
    {
54
        return $this->event;
55 1
    }
56
57
    final public function setStatusCode(int $statusCode): static
58 1
    {
59
        $this->statusCode = $statusCode;
60 1
        return $this;
61 1
    }
62
63
    final public function getStatusCode(): int
64 2
    {
65
        return $this->statusCode;
66 2
    }
67
68
    final public function addHeader(string $name, $value): static
69 1
    {
70
        $this->headers[$name] = $value;
71 1
        return $this;
72 1
    }
73
74
    final public function getHeaders(): array
75 2
    {
76
        return array_merge($this->headers, $this->requiredHeaders);
77 2
    }
78
79
    final public function addBodyMeta(string $name, $value): static
80 1
    {
81
        if (!array_key_exists('meta', $this->body)) {
82 1
            $this->body['meta'] = [];
83 1
        }
84
        $this->body['meta'][$name] = $value;
85
        return $this;
86 1
    }
87
88 1
    final public function setBodyData(array $value): static
89 1
    {
90
        $this->body['data'] = $value;
91
        return $this;
92 1
    }
93
94 1
    final public function getBody(): array
95
    {
96
        return $this->body;
97 1
    }
98
99 1
    final public function send(): array
100
    {
101 1
        $this->handle();
102 1
        $response = [
103 1
            'statusCode' => $this->getStatusCode(),
104
            'headers' => $this->getHeaders(),
105
        ];
106
        if ($this->body) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->body of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
107 1
            $response['body'] = json_encode($this->getBody());
108
        }
109
        return $response;
110 1
    }
111
112
    public function handle(): void
113
    {
114 1
        $songs = [
115 1
            ['artist' => 'Afta-1', 'songTitle' => 'Quest', 'albumTitle' => 'Aftathoughts Vol. 1'],
116 1
            ['artist' => 'Eric Lau', 'songTitle' => 'Cloud Burst', 'albumTitle' => 'Quadrivium'],
117
            ['artist' => 'Dr. Who Dat?', 'songTitle' => 'Braziliant Thought', 'albumTitle' => 'Beat Journey'],
118
        ];
119
        $this->addBodyMeta('count', count($songs));
120
        $this->setBodyData($songs);
121
    }
122
}
123