JsonResponder::success()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1
1
<?php namespace Arcanedev\LaravelApiHelper\Http;
2
3
use Arcanedev\LaravelApiHelper\Contracts\Http\JsonResponse as JsonResponseContract;
4
use Illuminate\Contracts\Routing\ResponseFactory;
5
6
/**
7
 * Class     JsonResponder
8
 *
9
 * @package  Arcanedev\LaravelApiHelper\Http
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class JsonResponder implements JsonResponseContract
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * The response factory.
21
     *
22
     * @var  \Illuminate\Contracts\Routing\ResponseFactory
23
     */
24
    protected $response;
25
26
    /* -----------------------------------------------------------------
27
     |  Constructor
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * JsonResponse constructor.
33
     *
34
     * @param  \Illuminate\Contracts\Routing\ResponseFactory  $response
35
     */
36 28
    public function __construct(ResponseFactory $response)
37
    {
38 28
        $this->response = $response;
39 28
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Main Methods
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Respond with a success response.
48
     *
49
     * @param  array   $data
50
     * @param  int     $status
51
     * @param  string  $code
52
     * @param  array   $headers
53
     * @param  int     $options
54
     *
55
     * @return \Illuminate\Http\JsonResponse
56
     */
57 16
    public function success(array $data = [], $status = 200, $code = 'success', array $headers = [], $options = 0)
58
    {
59 16
        return $this->respond($data, $status, $code, $headers, $options);
60
    }
61
62
    /**
63
     * Respond with an error response.
64
     *
65
     * @param  array   $data
66
     * @param  int     $status
67
     * @param  string  $code
68
     * @param  array   $headers
69
     * @param  int     $options
70
     *
71
     * @return \Illuminate\Http\JsonResponse
72
     */
73 12
    public function error(array $data = [], $status = 400, $code = 'error', array $headers = [], $options = 0)
74
    {
75 12
        return $this->respond($data, $status, $code, $headers, $options);
76
    }
77
78
    /**
79
     * Respond with a json response.
80
     *
81
     * @param  array   $data
82
     * @param  int     $status
83
     * @param  string  $code
84
     * @param  array   $headers
85
     * @param  int     $options
86
     *
87
     * @return \Illuminate\Http\JsonResponse
88
     */
89 28
    public function respond(array $data, $status = 200, $code, array $headers = [], $options = 0)
90
    {
91 28
        return $this->response->json(
92 28
            array_merge(compact('status', 'code'), $data), $status, $headers, $options
93
        );
94
    }
95
}
96