Completed
Push — master ( ec71aa...2c64ed )
by Ruben
01:09
created

HasEndpoints::initializeEndpointResource()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 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|array $controller
13
     * @param null $parameters
14
     *
15
     * @return \Spatie\LaravelEndpointResources\EndpointResource
16
     */
17
    public function endpoints($controller = null, $parameters = null): EndpointResource
18
    {
19
        return EndpointResource::initialize($this->resource, EndpointResourceType::ITEM)->endpoint($controller, $parameters);
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...
20
    }
21
22
    /**
23
     * @param string|Closure|null|array $controller
24
     * @param null $parameters
25
     *
26
     * @return \Spatie\LaravelEndpointResources\EndpointResource
27
     */
28
    public static function collectionEndpoints($controller = null, $parameters = null): EndpointResource
29
    {
30
        return EndpointResource::initialize(null, EndpointResourceType::COLLECTION)->endpoint($controller, $parameters);
31
    }
32
33 View Code Duplication
    public static function collection($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $meta = self::meta();
36
37
        if (! count($meta)) {
38
            parent::collection($resource);
39
        }
40
41
        return parent::collection($resource)->additional([
42
            'meta' => $meta,
43
        ]);
44
    }
45
46 View Code Duplication
    public static function make(...$parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $meta = self::meta();
49
50
        if (! count($meta)) {
51
            parent::make(...$parameters);
52
        }
53
54
        return parent::make(...$parameters)->additional([
55
            'meta' => $meta,
56
        ]);
57
    }
58
59
    public static function meta()
60
    {
61
        return [];
62
    }
63
}
64