Completed
Pull Request — master (#83)
by
unknown
01:53
created

Fractal::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
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
     * Return a new JSON response.
19
     *
20
     * @param  callable|int $statusCode
21
     * @param  callable|array $headers
22
     *
23
     * @return \Illuminate\Http\JsonResponse
24
     */
25
    public function respond($statusCode = 200, $headers = [])
26
    {
27
        $response = new JsonResponse();
28
29
        $response->setData($this->createData()->toArray());
30
31
        if (is_int($statusCode)) {
32
            $statusCode = function (JsonResponse $response) use ($statusCode) {
33
                return $response->setStatusCode($statusCode);
34
            };
35
        }
36
37
        if (is_array($headers)) {
38
            $headers = function (JsonResponse $response) use ($headers) {
39
                return $response->withHeaders($headers);
40
            };
41
        }
42
43
        if (is_callable($statusCode)) {
44
            $statusCode($response);
45
        }
46
47
        if (is_callable($headers)) {
48
            $headers($response);
49
        }
50
51
        return $response;
52
    }
53
54
    /**
55
     * @param null|mixed $data
56
     * @param null|callable|\League\Fractal\TransformerAbstract $transformer
57
     * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer
58
     *
59
     * @return \Spatie\Fractalistic\Fractal
60
     */
61
    public static function create($data = null, $transformer = null, $serializer = null)
62
    {
63
        $fractal = parent::create($data, $transformer, $serializer);
64
65
        $serializer = config('laravel-fractal.default_serializer');
66
67
        if (! empty($serializer)) {
68
            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...
69
                $fractal->serializeWith($serializer);
70
            } else {
71
                $fractal->serializeWith(new $serializer());
72
            }
73
        }
74
75
        return $fractal;
76
    }
77
}
78