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

HasEndpoints   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 90
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A endpoints() 0 8 1
A collectionEndpoints() 0 8 1
A collection() 0 12 2
A make() 0 12 2
A meta() 0 4 1
A initializeEndpointResource() 0 19 3
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