Completed
Push — master ( 2d5c6f...ea456d )
by Alexander
06:30
created

ResponseFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Factories;
4
5
use Illuminate\Http\JsonResponse;
6
7
/**
8
 *
9
 *
10
 * @package Laravel Responder
11
 * @author  Alexander Tømmerås <[email protected]>
12
 * @license The MIT License
13
 */
14
abstract class ResponseFactory
15
{
16
    /**
17
     * @var bool
18
     */
19
    protected $includeStatusCode = true;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param bool $includeStatusCode
25
     */
26
    public function __construct( bool $includeStatusCode )
27
    {
28
        $this->includeStatusCode = $includeStatusCode;
29
    }
30
31
    /**
32
     * Generate a JSON response.
33
     *
34
     * @param  array $data
35
     * @param  int   $statusCode
36
     * @return JsonResponse
37
     */
38
    public function make( $data, $statusCode ):JsonResponse
39
    {
40
        $data = $this->includeStatusCode( $statusCode, $data );
41
42
        return response()->json( $data, $statusCode );
43
    }
44
45
    /**
46
     * Prepend a status code to the response data if status codes are enabled.
47
     *
48
     * @param  int   $statusCode
49
     * @param  array $data
50
     * @return array
51
     */
52
    protected function includeStatusCode( int $statusCode, array $data ):array
53
    {
54
        if ( ! $this->includeStatusCode ) {
55
            return $data;
56
        }
57
58
        return array_merge( [
59
            'status' => $statusCode
60
        ], $data );
61
    }
62
}