Completed
Push — main ( def8a0...e32e37 )
by ABRAHAM
01:26
created

ApiResponse::methodNotAllowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Bytesfield\KeyManager\Traits;
4
5
use Illuminate\Http\JsonResponse;
6
use Bytesfield\KeyManager\Classes\Errors;
7
8
trait ApiResponse
9
{
10
    /**
11
     * Generates a not found response for a request.
12
     *
13
     * @param string $message
14
     * @return \Illuminate\Http\JsonResponse
15
     */
16
    public function notFound(string $message): JsonResponse
17
    {
18
        return $this->buildResponse($message, false, Errors::CODE['NOT_FOUND']);
19
    }
20
21
    /**
22
     * Generates a bad request response for a request.
23
     *
24
     * @param string $message
25
     * @return \Illuminate\Http\JsonResponse
26
     */
27
    public function badRequest(string $message): JsonResponse
28
    {
29
        return $this->buildResponse($message, false, Errors::CODE['BAD_REQUEST']);
30
    }
31
32
    /**
33
     * Generates a not found response for a request.
34
     *
35
     * @param string $message
36
     * @param array $errors
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function failedValidation(string $message, array $errors = []): JsonResponse
40
    {
41
        return $this->buildResponse($message, false, Errors::CODE['VALIDATION_ERROR'], $errors);
42
    }
43
44
    /**
45
     * Generates an unauthorized response for a request.
46
     *
47
     * @param string $message.
0 ignored issues
show
Documentation introduced by
There is no parameter named $message.. Did you maybe mean $message?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
48
     * @return \Illuminate\Http\JsonResponse
49
     */
50
    public function unauthorized(string $message): JsonResponse
51
    {
52
        return $this->buildResponse($message, false, Errors::CODE['UNAUTHORIZED']);
53
    }
54
55
    /**
56
     * Generates a method not found response for a request.
57
     *
58
     * @param string $message
59
     * @return \Illuminate\Http\JsonResponse
60
     */
61
    public function methodNotAllowed(string $message): JsonResponse
62
    {
63
        return $this->buildResponse($message, false, Errors::CODE['METHOD_NOT_FOUND']);
64
    }
65
66
    /**
67
     * Generates a failed Data Creation response for a request.
68
     *
69
     * @param string $message
70
     * @return \Illuminate\Http\JsonResponse
71
     */
72
    public function failedDataCreation(string $message): JsonResponse
73
    {
74
        return $this->buildResponse($message, false, Errors::CODE['BAD_REQUEST']);
75
    }
76
77
    /**
78
     * Generates a success response for a request.
79
     *
80
     * @param string $message
81
     * @param array $data
82
     * @return \Illuminate\Http\JsonResponse
83
     */
84
    public function success(string $message, array $data = []): JsonResponse
85
    {
86
        return $this->buildResponse($message, true, Errors::CODE['OK'], $data);
87
    }
88
89
    /**
90
     * Generates a success response for a request.
91
     *
92
     * @param string $message
93
     * @param array $data
94
     * @return \Illuminate\Http\JsonResponse
95
     */
96
    public function actionSuccess(string $message, array $data = []): JsonResponse
97
    {
98
        return $this->buildResponse($message, true, Errors::CODE['OK'], $data);
99
    }
100
101
    /**
102
     * Generates an error response for a request.
103
     *
104
     * @param string $message
105
     * @return \Illuminate\Http\JsonResponse
106
     */
107
    public function error(string $message): JsonResponse
108
    {
109
        return $this->buildResponse($message, false, Errors::CODE['CONFLICT']);
110
    }
111
112
    /**
113
     * Generates a server error response for a request.
114
     *
115
     * @param string $message
116
     * @return \Illuminate\Http\JsonResponse
117
     */
118
    public function serverError(string $message): JsonResponse
119
    {
120
        return $this->buildResponse($message, false, Errors::CODE['SERVER_ERROR']);
121
    }
122
123
    /**
124
     * Generates a forbidden response for a request.
125
     *
126
     * @param string $message
127
     *
128
     * @return \Illuminate\Http\JsonResponse
129
     */
130
    public function forbidden(string $message): JsonResponse
131
    {
132
        return $this->buildResponse($message, false, Errors::CODE['FORBIDDEN']);
133
    }
134
135
    /**
136
     * Built a response for a request.
137
     *
138
     * @param string $message
139
     * @param bool $status
140
     * @param int $statusCode
141
     * @param array $data
142
     * @param array $headers
143
     * @return \Illuminate\Http\JsonResponse
144
     */
145
    private function buildResponse(
146
        string $message,
147
        bool $status,
148
        int $statusCode,
149
        array $data = [],
150
        array $headers = []
0 ignored issues
show
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    ) {
152
        $responseData = [
153
            'status' => $status,
154
            'statusCode' => $statusCode,
155
            'message' => $message,
156
        ];
157
158
        if (! empty($data)) {
159
            $responseData['data'] = $data;
160
        }
161
162
        return new JsonResponse($responseData, $statusCode);
163
    }
164
}
165