Completed
Push — master ( 445364...d7ac9b )
by Freek
10:05 queued 08:30
created

Fractal::create()   D

Complexity

Conditions 10
Paths 90

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 90
nop 3
dl 0
loc 46
rs 4.983

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     *
83
     * @return \Illuminate\Http\JsonResponse
84
     */
85
    public function respond($statusCode = 200, $headers = [])
86
    {
87
        $response = new JsonResponse();
88
89
        $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...
90
91
        if (is_int($statusCode)) {
92
            $statusCode = function (JsonResponse $response) use ($statusCode) {
93
                return $response->setStatusCode($statusCode);
94
            };
95
        }
96
97
        if (is_array($headers)) {
98
            $headers = function (JsonResponse $response) use ($headers) {
99
                return $response->withHeaders($headers);
100
            };
101
        }
102
103
        if (is_callable($statusCode)) {
104
            $statusCode($response);
105
        }
106
107
        if (is_callable($headers)) {
108
            $headers($response);
109
        }
110
111
        return $response;
112
    }
113
}
114