Completed
Push — master ( 834214...ebf38e )
by Ruben
01:35
created

HasEndpoints::meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\LaravelEndpointResources;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
8
/** @mixin \Illuminate\Http\Resources\Json\JsonResource */
9
trait HasEndpoints
10
{
11
    /**
12
     * @param string|Closure|null $controller
13
     * @param null $parameters
14
     *
15
     * @return \Spatie\LaravelEndpointResources\EndpointResource
16
     */
17
    public function endpoints($controller = null, $parameters = null): EndpointResource
18
    {
19
        return self::initializeEndpointResource(
20
            $endPointResource = new EndpointResource($this->resource, EndpointResourceType::ITEM),
0 ignored issues
show
Bug introduced by
The property resource does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
            $controller,
22
            $parameters
23
        );
24
    }
25
26
    /**
27
     * @param string|Closure|null $controller
28
     * @param null $parameters
29
     *
30
     * @return \Spatie\LaravelEndpointResources\EndpointResource
31
     */
32
    public static function collectionEndpoints($controller = null, $parameters = null): EndpointResource
33
    {
34
        return self::initializeEndpointResource(
35
            $endPointResource = new EndpointResource(null, EndpointResourceType::COLLECTION),
36
            $controller,
37
            $parameters
38
        );
39
    }
40
41
    public static function collection($resource)
42
    {
43
        $meta = self::meta();
44
45
        if (! count($meta)) {
46
            parent::collection($resource);
47
        }
48
49
        return parent::collection($resource)->additional([
50
            'meta' => $meta,
51
        ]);
52
    }
53
54
    public static function make(...$parameters)
55
    {
56
        $meta = self::meta();
57
58
        if (! count($meta)) {
59
            parent::make(...$parameters);
60
        }
61
62
        return parent::make(...$parameters)->additional([
63
            'meta' => $meta,
64
        ]);
65
    }
66
67
    public static function meta()
68
    {
69
        return [];
70
    }
71
72
    /**
73
     * @param \Spatie\LaravelEndpointResources\EndpointResource|null $endpointResource
74
     * @param string|Closure|null $controller
75
     * @param null $parameters
76
     *
77
     * @return \Spatie\LaravelEndpointResources\EndpointResource
78
     */
79
    private static function initializeEndpointResource(
80
        ?EndpointResource $endpointResource,
81
        $controller = null,
82
        $parameters = null
83
    ): EndpointResource {
84
        if ($controller instanceof Closure) {
85
            $endpointsCollection = new EndpointsCollection();
86
87
            $controller($endpointsCollection);
88
89
            return $endpointResource->addEndpointsCollection($endpointsCollection);
0 ignored issues
show
Bug introduced by
It seems like $endpointResource is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
90
        }
91
92
        if ($controller !== null) {
93
            return $endpointResource->addController($controller, Arr::wrap($parameters));
94
        }
95
96
        return $endpointResource;
97
    }
98
}
99