Completed
Push — master ( 34a725...c29829 )
by Freek
01:28
created

Fractal::respond()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 1
b 0
f 0
nc 64
nop 3
dl 0
loc 40
rs 6.7272
1
<?php
2
3
namespace Spatie\Fractal;
4
5
use Closure;
6
use League\Fractal\Manager;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\JsonResponse;
9
use League\Fractal\Serializer\JsonApiSerializer;
10
use Spatie\Fractalistic\Fractal as Fractalistic;
11
use League\Fractal\Serializer\SerializerAbstract;
12
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
14
15
class Fractal extends Fractalistic
16
{
17
    /** @param \League\Fractal\Manager $manager */
18
    public function __construct(Manager $manager)
19
    {
20
        parent::__construct($manager);
21
    }
22
23
    /**
24
     * @param null|mixed $data
25
     * @param null|callable|\League\Fractal\TransformerAbstract $transformer
26
     * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer
27
     *
28
     * @return \Spatie\Fractalistic\Fractal
29
     */
30
    public static function create($data = null, $transformer = null, $serializer = null)
31
    {
32
        $fractal = parent::create($data, $transformer, $serializer);
33
34
        if (config('fractal.auto_includes.enabled')) {
35
            $requestKey = config('fractal.auto_includes.request_key');
36
37
            if (request()->query($requestKey)) {
38
                $fractal->parseIncludes(explode(',', request()->query($requestKey)));
39
            }
40
        }
41
42
        if (empty($serializer)) {
43
            $serializer = config('fractal.default_serializer');
44
        }
45
46
        if ($data instanceof LengthAwarePaginator) {
47
            $paginator = config('fractal.default_paginator');
48
49
            if (empty($paginator)) {
50
                $paginator = IlluminatePaginatorAdapter::class;
51
            }
52
53
            $fractal->paginateWith(new $paginator($data));
54
        }
55
56
        if (empty($serializer)) {
57
            return $fractal;
58
        }
59
60
        if ($serializer instanceof SerializerAbstract) {
61
            return $fractal->serializeWith($serializer);
62
        }
63
64
        if ($serializer instanceof Closure) {
65
            return $fractal->serializeWith($serializer());
66
        }
67
68
        if ($serializer == JsonApiSerializer::class) {
69
            $baseUrl = config('fractal.base_url');
70
71
            return $fractal->serializeWith(new $serializer($baseUrl));
72
        }
73
74
        return $fractal->serializeWith(new $serializer);
75
    }
76
77
    /**
78
     * Return a new JSON response.
79
     *
80
     * @param  callable|int $statusCode
81
     * @param  callable|array $headers
82
     * @param  callable|int $options
83
     *
84
     * @return \Illuminate\Http\JsonResponse
85
     */
86
    public function respond($statusCode = 200, $headers = [], $options = 0)
87
    {
88
        $response = new JsonResponse();
89
90
        $response->setData($this->createData()->toArray());
0 ignored issues
show
Bug introduced by
It seems like $this->createData()->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Illuminate\Http\JsonResponse::setData() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
91
92
        if (is_int($statusCode)) {
93
            $statusCode = function (JsonResponse $response) use ($statusCode) {
94
                return $response->setStatusCode($statusCode);
95
            };
96
        }
97
98
        if (is_array($headers)) {
99
            $headers = function (JsonResponse $response) use ($headers) {
100
                return $response->withHeaders($headers);
101
            };
102
        }
103
104
        if (is_int($options)) {
105
            $options = function (JsonResponse $response) use ($options) {
106
                $response->setEncodingOptions($options);
107
108
                return $response;
109
            };
110
        }
111
112
        if (is_callable($options)) {
113
            $options($response);
114
        }
115
116
        if (is_callable($statusCode)) {
117
            $statusCode($response);
118
        }
119
120
        if (is_callable($headers)) {
121
            $headers($response);
122
        }
123
124
        return $response;
125
    }
126
}
127