Response::successfullResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Rest Response
5
 *
6
 * @link        https://github.com/MelquesPaiva/rest-response for the canonical source repository
7
 * @copyright   Copyright (c) MelquesPaiva
8
 */
9
10
namespace MelquesPaiva\RestResponse;
11
12
use MelquesPaiva\RestResponse\HttpResponse\Error;
13
use MelquesPaiva\RestResponse\HttpResponse\Success;
14
15
/**
16
 * Class Response
17
 * @package MelquesPaiva\RestResponse
18
 */
19
class Response
20
{
21
    /** @var Error */
22
    protected Error $error;
23
24
    /** @var Success */
25
    protected Success $success;
26
27
    /**
28
     * Response Constructor
29
     */
30
    public function __construct()
31
    {
32
        $this->error = new Error();
33
        $this->success = new Success();
34
    }
35
36
    /**
37
     * Function responsible for bad request errors (statusCode = 400)
38
     * 
39
     * @param string $message
40
     * @param string $parameter
41
     * @return void
42
     */
43
    public function badRequest(string $message, string $parameter = ""): void
44
    {
45
        echo $this->errorResponse($this->error->badRequest($message, $parameter));
46
    }
47
48
    /**
49
     * Function responsible for handle authentication errors (statusCode = 401)
50
     * 
51
     * @param string $parameter
52
     * @param string $message
53
     * @return void
54
     */
55
    public function unauthorized(string $message, string $parameter = ""): void 
56
    {
57
        echo $this->errorResponse($this->error->unauthorized($message, $parameter));
58
    }
59
60
    /**
61
     * Function responsible for handle action_forbidden errors (statusCode = 403)
62
     * 
63
     * @param string $message
64
     * @return void
65
     */
66
    public function actionForbidden(string $message): void
67
    {
68
        echo $this->errorResponse($this->error->actionForbidden($message));
69
    }
70
71
    /**
72
     * Function responsible for notFound data response (statusCode = 404)
73
     *
74
     * @param string $message
75
     * @param string $paramenter
76
     * @return void
77
     */
78
    public function notFound(string $message, string $paramenter = ""): void
79
    {
80
        echo $this->errorResponse($this->error->notFound($message, $paramenter));
81
    }
82
83
    /**
84
     * Function responsible for method not allowed errors (statusCode = 405)
85
     * 
86
     * @return void
87
     */
88
    public function methodNotAllowed(string $message, string $parameter = ""): void
89
    {
90
        echo $this->errorResponse($this->error->methodNotAllowed($message, $parameter));
91
    }
92
93
    /**
94
     * Function responsible for internal errors (statusCode = 500)
95
     * 
96
     * @param string $message
97
     * @return void
98
     */
99
    public function internalError(string $message): void
100
    {
101
        echo $this->errorResponse($this->error->internalError($message));
102
    }
103
104
    /**
105
     * Function responsible for methodNotImplemented errors (statusCode = 501)
106
     * 
107
     * @param string $message
108
     * @return void
109
     */
110
    public function methodNotImplemented(string $message): void
111
    {
112
        echo $this->errorResponse($this->error->notImplemented($message));
113
    }
114
115
    /**
116
     * Function responsible for successful responses (statusCode = 200)
117
     * 
118
     * @param string $message
119
     * @param array $data
120
     * @return void
121
     */
122
    public function successful(string $message, array $data = []): void
123
    {
124
        echo $this->successfullResponse($this->success->success($message, $data));
125
    }
126
127
    /**
128
     * Function responsible for handle responses where nothing is found (statusCode = 204)
129
     * 
130
     * @return void
131
     */
132
    public function noContent(): void
133
    {
134
        echo $this->successfullResponse($this->success->noContent());
135
    }
136
137
    /**
138
     * Handle successfull responses
139
     *
140
     * @param Success $success
141
     * @return string
142
     */
143
    public function successfullResponse(Success $success): string
144
    {
145
        $return = $success->toArray();
146
147
        $return["meta"] = [
148
            "status" => $success->getStatusCode(),
149
            "url" => filter_input(INPUT_SERVER, 'REQUEST_URI'),
150
            "method" => filter_input(INPUT_SERVER, 'REQUEST_METHOD'),
151
            "error" => null
152
        ];
153
154
        return $this->httpResponse($return, $success->getStatusCode());
155
    }
156
157
    /**
158
     * Handle Error Responses
159
     *
160
     * @param Error $error
161
     * @return string
162
     */
163
    public function errorResponse(Error $error): string
164
    {
165
        $return = [];
166
        $return["meta"] = [
167
            "status" => $error->getStatusCode(),
168
            "url" => filter_input(INPUT_SERVER, 'REQUEST_URI'),
169
            "method" => filter_input(INPUT_SERVER, 'REQUEST_METHOD'),
170
            "error" => $error->toArray()
171
        ];
172
173
        return $this->httpResponse($return, $error->getStatusCode());
174
    }
175
176
    /**
177
     * Function responsible to manage http responses
178
     *
179
     * @param array $return
180
     * @param int $statusCode
181
     * @return string
182
     */
183
    protected function httpResponse(array $return, int $statusCode): string
184
    {
185
        http_response_code($statusCode);
186
        return json_encode($return, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
187
    }
188
}
189