Completed
Push — master ( afd0d4...82df1a )
by Freek
01:53
created

Fractal::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Fractal;
4
5
use League\Fractal\Manager;
6
use Illuminate\Http\JsonResponse;
7
use Spatie\Fractalistic\Fractal as Fractalistic;
8
9
class Fractal extends Fractalistic
10
{
11
    /** @param \League\Fractal\Manager $manager */
12
    public function __construct(Manager $manager)
13
    {
14
        parent::__construct($manager);
15
    }
16
17
    /**
18
     * @param null|mixed $data
19
     * @param null|callable|\League\Fractal\TransformerAbstract $transformer
20
     * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer
21
     *
22
     * @return \Spatie\Fractalistic\Fractal
23
     */
24
    public static function create($data = null, $transformer = null, $serializer = null)
25
    {
26
        $fractal = parent::create($data, $transformer, $serializer);
27
28
        $serializer = config('laravel-fractal.default_serializer');
29
30
        if (empty($serializer)) {
31
            return $fractal;
32
        }
33
34
        if ($serializer instanceof SerializerAbstract) {
0 ignored issues
show
Bug introduced by
The class Spatie\Fractal\SerializerAbstract does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
35
            return $fractal->serializeWith($serializer);
36
        }
37
38
        return $fractal->serializeWith(new $serializer());
39
    }
40
41
    /**
42
     * Return a new JSON response.
43
     *
44
     * @param  callable|int $statusCode
45
     * @param  callable|array $headers
46
     *
47
     * @return \Illuminate\Http\JsonResponse
48
     */
49
    public function respond($statusCode = 200, $headers = [])
50
    {
51
        $response = new JsonResponse();
52
53
        $response->setData($this->createData()->toArray());
54
55
        if (is_int($statusCode)) {
56
            $statusCode = function (JsonResponse $response) use ($statusCode) {
57
                return $response->setStatusCode($statusCode);
58
            };
59
        }
60
61
        if (is_array($headers)) {
62
            $headers = function (JsonResponse $response) use ($headers) {
63
                return $response->withHeaders($headers);
64
            };
65
        }
66
67
        if (is_callable($statusCode)) {
68
            $statusCode($response);
69
        }
70
71
        if (is_callable($headers)) {
72
            $headers($response);
73
        }
74
75
        return $response;
76
    }
77
78
79
}
80