Passed
Push — main ( 4d5870...feac8a )
by Mojtaba
02:07
created

ApiResponseHelper::respondWithSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bugloos\ResponderBundle\ResponseHelper;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
use Symfony\Component\HttpFoundation\Response;
7
8
/**
9
 * @author Mojtaba Gheytasi <[email protected]>
10
 */
11
trait ApiResponseHelper
12
{
13
    private int $statusCode;
14
15
    protected function respondDeleted(
16
        string $message,
17
        array $headers = []
18
    ): JsonResponse {
19
        return $this->setStatusCode(Response::HTTP_OK)
20
            ->respondWithMessage($message, $headers);
21
    }
22
23
    protected function respondNoContent(array $headers = []): JsonResponse
24
    {
25
        return $this->setStatusCode(Response::HTTP_NO_CONTENT)
26
            ->respond([], $headers, []);
27
    }
28
29
    protected function respondForbiddenError(
30
        string $message,
31
        array $headers = []
32
    ): JsonResponse {
33
        return $this->setStatusCode(Response::HTTP_FORBIDDEN)
34
            ->respondWithMessage($message, $headers);
35
    }
36
37
    protected function respondNotFoundError(
38
        string $message,
39
        array $headers = []
40
    ): JsonResponse {
41
        return $this->setStatusCode(Response::HTTP_NOT_FOUND)
42
            ->respondWithMessage($message, $headers);
43
    }
44
45
    protected function respondUnauthorizedError(
46
        string $message,
47
        array $headers = []
48
    ): JsonResponse {
49
        return $this->setStatusCode(Response::HTTP_UNAUTHORIZED)
50
            ->respondWithMessage($message, $headers);
51
    }
52
53
    protected function respondWithSuccessMessage(
54
        string $message,
55
        array $headers = []
56
    ): JsonResponse {
57
        return $this->setStatusCode(Response::HTTP_OK)
58
            ->respondWithMessage($message, $headers);
59
    }
60
61
    /** new functions - start */
62
63
    protected function respondWithSuccess(
64
        array|object $data,
65
        array $groups= [],
66
        array $headers = []
67
    ): JsonResponse {
68
        return $this->setStatusCode(Response::HTTP_OK)
69
            ->respond($data, $groups, $headers);
70
    }
71
72
    protected function respondCreated(
73
        array|object $data,
74
        array $groups= [],
75
        array $headers = []
76
    ): JsonResponse {
77
        return $this->setStatusCode(Response::HTTP_CREATED)
78
            ->respond($data, $groups, $headers);
79
    }
80
81
    protected function respondCreatedMessage(
82
        string $message,
83
        array $headers = []
84
    ): JsonResponse {
85
        return $this->setStatusCode(Response::HTTP_CREATED)
86
            ->respondWithMessage($message, $headers);
87
    }
88
89
    /**
90
     * @deprecated since Responder Bundle v1.0.0, use respondWithSuccess() instead
91
     */
92
    protected function respondWithItem(
93
        $item,
94
        array $groups = [],
95
        array $headers = []
96
    ): JsonResponse {
97
98
        trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccess()" instead.', __METHOD__);
99
100
        return $this->setStatusCode(Response::HTTP_OK)
101
            ->respond($item, $groups, $headers);
102
    }
103
104
    /**
105
     * @deprecated since Responder Bundle v1.0.0, use respondWithSuccess() instead
106
     */
107
    protected function respondWithCollection(
108
        $collection,
109
        array $groups = [],
110
        array $headers = []
111
    ): JsonResponse {
112
113
        trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccess()" instead.', __METHOD__);
114
115
        return $this->setStatusCode(Response::HTTP_OK)
116
            ->respond($collection, $groups, $headers);
117
    }
118
119
    /**
120
     * @deprecated since Responder Bundle v1.0.0, use respondWithSuccess() instead
121
     */
122
    protected function respondWithPagination(
123
        $pagination,
124
        array $groups = [],
125
        array $headers = []
126
    ): JsonResponse {
127
128
        trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccess()" instead.', __METHOD__);
129
130
        return $this->setStatusCode(Response::HTTP_OK)
131
            ->respond($pagination, $groups, $headers);
132
    }
133
134
    /**
135
     * @deprecated since Responder Bundle v1.0.0, use respondWithSuccessMessage() instead
136
     */
137
    protected function respondItemUpdated(
138
        $message,
139
        array $headers = []
140
    ): JsonResponse {
141
142
        trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondWithSuccessMessage()" instead.', __METHOD__);
143
144
        return $this->setStatusCode(Response::HTTP_OK)
145
            ->respondWithMessage($message, $headers);
146
    }
147
148
149
    /**
150
     * @deprecated since Responder Bundle v1.0.0, use respondCreated() OR respondCreatedMessage() instead
151
     */
152
    protected function respondItemCreated(
153
        string $message,
154
        array $headers = []
155
    ): JsonResponse {
156
157
        trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondCreated()" OR "respondCreatedMessage()" instead.', __METHOD__);
158
159
        return $this->setStatusCode(Response::HTTP_CREATED)
160
            ->respondWithMessage($message, $headers);
161
    }
162
163
    /**
164
     * @deprecated since Responder Bundle v1.0.0, use respondDeleted() instead
165
     */
166
    protected function respondItemDeleted(
167
        string $message,
168
        array $headers = []
169
    ): JsonResponse {
170
171
        trigger_deprecation('bugloos/responder-bundle', '1.0.0', 'The "%s()" method is deprecated, use "respondDeleted()" instead.', __METHOD__);
172
173
        return $this->setStatusCode(Response::HTTP_OK)
174
            ->respondWithMessage($message, $headers);
175
    }
176
177
    private function setStatusCode(int $statusCode): self
178
    {
179
        $this->statusCode = $statusCode;
180
181
        return $this;
182
    }
183
184
    private function getStatusCode(): string
185
    {
186
        return $this->statusCode;
187
    }
188
189
    private function respond($data, array $groups, array $headers): JsonResponse
190
    {
191
        return $this->json(
0 ignored issues
show
Bug introduced by
It seems like json() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
        return $this->/** @scrutinizer ignore-call */ json(
Loading history...
192
            $data,
193
            $this->getStatusCode(),
194
            $headers,
195
            ['groups' => $groups]
196
        );
197
    }
198
199
    private function respondWithMessage(string $message, array $headers = []): JsonResponse
200
    {
201
        return $this->respond([
202
            'message' => $message,
203
        ], $headers, []);
204
    }
205
206
}
207