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

JsonApiResponse   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 4
dl 0
loc 19
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 9 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
}