Passed
Push — master ( a4e975...813b52 )
by Arthur
03:12
created

JsonApiResponse::getResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace SoliDry\Helpers;
4
5
6
use SoliDry\Extension\JSONApiInterface;
7
use Illuminate\Http\Response;
8
9
class JsonApiResponse
10
{
11
12
    /**
13
     * Prepares Response object to return with particular http response, headers and body
14
     *
15
     * @param string $json
16
     * @param int $responseCode
17
     * @return Response
18
     */
19
    public function getResponse(string $json, int $responseCode = JSONApiInterface::HTTP_RESPONSE_CODE_OK) : Response
20
    {
21
        if ($responseCode === JSONApiInterface::HTTP_RESPONSE_CODE_NO_CONTENT) {
22
            // ! it is important to pass empty string here, otherwise there will be Exception from ResponseFactory
23
            // seems like a bug in Laravel - either don't have time to report them and don't like their PR rules
24
            return response('')->withHeaders(JSONApiInterface::STANDARD_HEADERS)->setStatusCode($responseCode);
25
        }
26
27
        return response($json)->withHeaders(JSONApiInterface::STANDARD_HEADERS)->setStatusCode($responseCode);
28
    }
29
}