Completed
Pull Request — master (#96)
by
unknown
05:11
created

Fractal::create()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 24
rs 8.5125
1
<?php
2
3
namespace Spatie\Fractal;
4
5
use Closure;
6
use League\Fractal\Manager;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
10
use Spatie\Fractalistic\Fractal as Fractalistic;
11
use League\Fractal\Serializer\SerializerAbstract;
12
13
class Fractal extends Fractalistic
14
{
15
    /** @param \League\Fractal\Manager $manager */
16
    public function __construct(Manager $manager)
17
    {
18
        parent::__construct($manager);
19
    }
20
21
    /**
22
     * @param null|mixed $data
23
     * @param null|callable|\League\Fractal\TransformerAbstract $transformer
24
     * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer
25
     *
26
     * @return \Spatie\Fractalistic\Fractal
27
     */
28
    public static function create($data = null, $transformer = null, $serializer = null)
29
    {
30
        $fractal = parent::create($data, $transformer, $serializer);
31
32
        $serializer = config('laravel-fractal.default_serializer');
33
34
        if($data instanceof LengthAwarePaginator) {
35
            $fractal->paginateWith(new IlluminatePaginatorAdapter($data));
36
        }
37
38
        if (empty($serializer)) {
39
            return $fractal;
40
        }
41
42
        if ($serializer instanceof SerializerAbstract) {
43
            return $fractal->serializeWith($serializer);
44
        }
45
46
        if ($serializer instanceof Closure) {
47
            return $fractal->serializeWith($serializer());
48
        }
49
50
        return $fractal->serializeWith(new $serializer());
51
    }
52
53
    /**
54
     * Return a new JSON response.
55
     *
56
     * @param  callable|int $statusCode
57
     * @param  callable|array $headers
58
     *
59
     * @return \Illuminate\Http\JsonResponse
60
     */
61
    public function respond($statusCode = 200, $headers = [])
62
    {
63
        $response = new JsonResponse();
64
65
        $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...
66
67
        if (is_int($statusCode)) {
68
            $statusCode = function (JsonResponse $response) use ($statusCode) {
69
                return $response->setStatusCode($statusCode);
70
            };
71
        }
72
73
        if (is_array($headers)) {
74
            $headers = function (JsonResponse $response) use ($headers) {
75
                return $response->withHeaders($headers);
76
            };
77
        }
78
79
        if (is_callable($statusCode)) {
80
            $statusCode($response);
81
        }
82
83
        if (is_callable($headers)) {
84
            $headers($response);
85
        }
86
87
        return $response;
88
    }
89
}
90