ApiResponse::buildResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MedianetDev\LaravelAuthApi\Http\Helpers;
4
5
use Symfony\Component\HttpFoundation\Response as ResponseCode;
6
7
class ApiResponse
8
{
9
    /**
10
     * @var array
11
     */
12
    private $data;
13
14
    /**
15
     * @var bool
16
     */
17
    private $status;
18
19
    /**
20
     * @var int
21
     */
22
    private $responseCode;
23
24
    /**
25
     * @var string
26
     */
27
    private $message;
28
29 5
    private function __construct($data, $status, $responseCode, $message)
30
    {
31 5
        $this->data = $data;
32 5
        $this->status = $status;
33 5
        $this->responseCode = $responseCode;
34 5
        $this->message = $message ?: ResponseCode::$statusTexts[$responseCode];
35 5
    }
36
37
    /**
38
     * setup, build and return the response.
39
     *
40
     * @param array  $data         An array of returned data
41
     * @param int    $status       true or false for the operation status
42
     * @param int    $responseCode the Http response code
43
     * @param string $message      response message
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47 5
    public static function send($data, $status = true, $responseCode = ResponseCode::HTTP_OK, string $message = '')
48
    {
49 5
        return (new static($data, $status, $responseCode, $message))->buildResponse();
50
    }
51
52
    /**
53
     * build the response.
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57 5
    private function buildResponse()
58
    {
59 5
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...), $this->responseCode) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
60 5
            'base_url' => \URL::to('/'),
61 5
            'status' => (bool) $this->status,
62 5
            'message' => $this->message,
63 5
            'data' => $this->data,
64 5
        ], $this->responseCode);
65
    }
66
}
67