ResponseHandler::respond()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 25
rs 8.5806
cc 4
eloc 18
nc 8
nop 7
1
<?php
2
3
namespace Vinelab\Api;
4
5
/**
6
 * @author  Mahmoud Zalt <[email protected]>
7
 * @author  Abed Halawi <[email protected]>
8
 */
9
class ResponseHandler
10
{
11
    /**
12
     * @var Responder instance
13
     */
14
    protected $responder;
15
16
    /**
17
     * @param \Vinelab\Api\Responder $responder
18
     */
19
    public function __construct(Responder $responder)
20
    {
21
        $this->responder = $responder;
22
    }
23
24
    /**
25
     * Format a response into an elegant api manager response.
26
     *
27
     * @param array $data
28
     * @param null  $total
29
     * @param null  $page
30
     * @param null  $per_page
31
     * @param int   $status
32
     * @param array $headers
33
     * @param int   $options
34
     *
35
     * @internal param $arguments
36
     *
37
     * @return \Illuminate\Http\JsonResponse
38
     */
39
    public function respond(
40
        array $data,
41
        $total = null,
42
        $page = null,
43
        $per_page = null,
44
        $status = 200,
45
        $headers = [],
46
        $options = 0
47
    ) {
48
        $response = [
49
            'status' => $status,
50
        ];
51
        if (!is_null($total)) {
52
            $response['total'] = $total;
53
        }
54
        if (!is_null($page)) {
55
            $response['page'] = $page;
56
        }
57
        if (!is_null($per_page)) {
58
            $response['per_page'] = $per_page;
59
        }
60
        $response['data'] = $data;
61
62
        return $this->responder->respond($response, $status, $headers, $options);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->responder->respon...s, $headers, $options); of type Vinelab\Api\Responder|Illuminate\Http\JsonResponse adds the type Vinelab\Api\Responder to the return on line 62 which is incompatible with the return type documented by Vinelab\Api\ResponseHandler::respond of type Illuminate\Http\JsonResponse.
Loading history...
63
    }
64
}
65