Completed
Push — master ( 2b9cec...803551 )
by Nassif
05:15 queued 03:08
created

ApiResponse::errorBadRequest()   A

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 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tequilarapido\ApiResponse;
4
5
use Illuminate\Support\MessageBag;
6
7
class ApiResponse
8
{
9
    /**
10
     * @var FractalAdapter
11
     */
12
    private $fractalAdapter;
13
14
    /**
15
     * Response class.
16
     *
17
     * @param FractalAdapter $fractal
18
     */
19 12
    public function __construct(FractalAdapter $fractal)
20
    {
21 12
        $this->fractalAdapter = $fractal;
22 12
    }
23
24
    /**
25
     * Returns one item.
26
     *
27
     * @param mixed $data
28
     * @param null  $transformer
29
     * @param null  $resourceKey
30
     * @param bool  $build
31
     *
32
     * @return array|\Illuminate\Http\Response|ResponseBuilder*
0 ignored issues
show
Documentation introduced by
The doc-type array|\Illuminate\Http\Response|ResponseBuilder* could not be parsed: Unknown type name "ResponseBuilder*" at position 32. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
33
     */
34 2 View Code Duplication
    public function item($data, $transformer = null, $resourceKey = null, $build = true)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36 2
        return $this->toResponseBuilder(
37 2
            $this->fractalAdapter->item($data, $transformer, $resourceKey),
38
            $build
39
        );
40
    }
41
42
    /**
43
     * Returns collection.
44
     *
45
     * @param      $data
46
     * @param null $transformer
47
     * @param null $resourceKey
48
     * @param bool $build
49
     *
50
     * @return array|\Illuminate\Http\Response|ResponseBuilder
51
     */
52 2 View Code Duplication
    public function collection($data, $transformer = null, $resourceKey = null, $build = true)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 2
        return $this->toResponseBuilder(
55 2
            $this->fractalAdapter->collection($data, $transformer, $resourceKey),
56
            $build
57
        );
58
    }
59
60
    /**
61
     * Returns paginated collection.
62
     *
63
     * @param      $paginator
64
     * @param null $transformer
65
     * @param null $resourceKey
66
     * @param bool $build
67
     *
68
     * @return array|\Illuminate\Http\Response|ResponseBuilder
69
     */
70 2 View Code Duplication
    public function paginatedCollection($paginator, $transformer = null, $resourceKey = null, $build = true)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72 2
        return $this->toResponseBuilder(
73 2
            $this->fractalAdapter->paginatedCollection($paginator, $transformer, $resourceKey),
74
            $build
75
        );
76
    }
77
78
    /**
79
     * Respond with a no content response.
80
     *
81
     * @param bool $build
82
     *
83
     * @return ResponseBuilder|\Illuminate\Http\Response
84
     */
85 1
    public function noContent($build = true)
86
    {
87 1
        $response = new ResponseBuilder(null);
88 1
        $response->setStatusCode(204);
89
90 1
        return $build ? $response->build() : $response;
91
    }
92
93
    /**
94
     * Return a 404 not found error.
95
     *
96
     * @param string|array $message
97
     * @param bool         $build
98
     *
99
     * @return \Illuminate\Http\Response
100
     */
101 1
    public function errorNotFound($message = 'Not Found', $build = true)
102
    {
103 1
        return $this->error($message, 404, null, $build);
104
    }
105
106
    /**
107
     * Return a 400 bad request error.
108
     *
109
     * @param string|array $message
110
     * @param bool         $build
111
     *
112
     * @return \Illuminate\Http\Response
113
     */
114 1
    public function errorBadRequest($message = 'Bad Request', $build = true)
115
    {
116 1
        return $this->error($message, 400, null, $build);
117
    }
118
119
    /**
120
     * Return a 401 unauthorized error.
121
     *
122
     * @param string|array $message
123
     * @param bool         $build
124
     *
125
     * @return \Illuminate\Http\Response
126
     */
127
    public function errorUnauthorized($message = 'Unauthorized', $build = true)
128
    {
129
        return $this->error($message, 401, null, $build);
130
    }
131
132
    /**
133
     * Return a 401 unauthorized error.
134
     *
135
     * @param string|array $message
136
     * @param bool         $build
137
     *
138
     * @return \Illuminate\Http\Response
139
     */
140 1
    public function errorForbidden($message = 'Forbidden', $build = true)
141
    {
142 1
        return $this->error($message, 403, null, $build);
143
    }
144
145
    /**
146
     * Return a 500 internal server error.
147
     *
148
     * @param string|array $message
149
     * @param bool         $build
150
     *
151
     * @return \Illuminate\Http\Response
152
     */
153 1
    public function errorInternal($message = 'Internal Error', $build = true)
154
    {
155 1
        return $this->error($message, 500, null, $build);
156
    }
157
158
    /**
159
     * Return an error response.
160
     *
161
     * @param                  $messages
162
     * @param int              $statusCode
163
     * @param mixed|MessageBag $errors
164
     * @param bool             $build
165
     *
166
     * @return \Illuminate\Http\Response
167
     */
168 5
    public function error($messages, $statusCode = 500, $errors = null, $build = true)
169
    {
170
        $error = [
171 5
            'error' => true,
172 5
            'code' => $statusCode,
173 5
            'message' => $messages,
174
        ];
175
176 5
        if (! is_null($errors)) {
177
            $error = array_merge($error, ['errors' => $errors]);
178
        }
179
180 5
        $response = (new ResponseBuilder($error))->setStatusCode($statusCode);
181
182 5
        return $build ? $response->build() : $response;
183
    }
184
185
    /**
186
     * Wrap into ResponseBuilder, and build if requested.
187
     * If not will return ResponseBuilder object, to be able to add headers for instance
188
     * before sending back response using `build()` method.
189
     *
190
     * @param $data
191
     * @param $build
192
     *
193
     * @return \Illuminate\Http\Response|ResponseBuilder
194
     */
195 6
    protected function toResponseBuilder($data, $build)
196
    {
197 6
        return $build
198 6
            ? (new ResponseBuilder($data))->build()
199 6
            : new ResponseBuilder($data);
200
    }
201
}
202