Completed
Push — master ( b14ac6...db0d94 )
by Ruben
01:12
created

EndpointResource::endpoint()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.7377
c 0
b 0
f 0
cc 6
nc 5
nop 3
1
<?php
2
3
namespace Spatie\LaravelResourceEndpoints;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Spatie\LaravelResourceEndpoints\EndpointTypes\ControllerEndpointType;
8
use Spatie\LaravelResourceEndpoints\EndpointTypes\EndpointType;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Http\Resources\Json\JsonResource;
11
12
class EndpointResource extends JsonResource
13
{
14
    /** @var string */
15
    private $endpointResourceType;
16
17
    /** @var \Spatie\LaravelResourceEndpoints\EndpointsGroup */
18
    private $endpointsGroup;
19
20
    public static function create(Model $model = null, string $endpointResourceType = null): EndpointResource
21
    {
22
        return new self($model, $endpointResourceType);
23
    }
24
25
    public function __construct(Model $model = null, string $endpointResourceType = null)
26
    {
27
        parent::__construct($model);
28
29
        $this->endpointResourceType = $endpointResourceType ?? EndpointResourceType::ITEM;
30
        $this->endpointsGroup = new EndpointsGroup();
31
    }
32
33
    public function endpoint($endpoint, $parameters = null, $httpVerb = null): EndpointResource
34
    {
35
        if ($endpoint instanceof Closure) {
36
            $endpoint($this->endpointsGroup);
37
38
            return $this;
39
        }
40
41
        if (is_array($endpoint)) {
42
            $this->endpointsGroup
43
                ->action($endpoint)
44
                ->httpVerb($httpVerb)
45
                ->parameters(Arr::wrap($parameters));
46
47
            return $this;
48
        }
49
50
        if (is_string($endpoint) && method_exists($endpoint, '__invoke')) {
51
            $this->endpointsGroup
52
                ->invokableController($endpoint)
53
                ->parameters(Arr::wrap($parameters));
54
55
            return $this;
56
        }
57
58
        if (is_string($endpoint)) {
59
            $this->endpointsGroup
60
                ->controller($endpoint)
61
                ->parameters(Arr::wrap($parameters));
62
63
            return $this;
64
        }
65
66
        return $this;
67
    }
68
69
    public function mergeCollectionEndpoints(): EndpointResource
70
    {
71
        $this->endpointResourceType = EndpointResourceType::MULTI;
72
73
        return $this;
74
    }
75
76
    public function toArray($request)
77
    {
78
        $this->ensureCollectionEndpointsAreAutomaticallyMerged();
79
80
        return $this->endpointsGroup
81
            ->getEndpointTypes()
82
            ->map(function (EndpointType $endpointType) use ($request) {
83
                return $endpointType->hasParameters() === false
84
                    ? $endpointType->parameters($request->route()->parameters())
85
                    : $endpointType;
86
            })
87
            ->mapWithKeys(function (EndPointType $endpointType) {
88
                if ($endpointType instanceof ControllerEndpointType) {
89
                    return $this->resolveEndpointsFromControllerEndpointType($endpointType);
90
                }
91
92
                return $endpointType->getEndpoints($this->resource);
93
            });
94
    }
95
96
    private function resolveEndpointsFromControllerEndpointType(ControllerEndpointType $endpointType): array
97
    {
98
        if ($this->endpointResourceType === EndpointResourceType::ITEM) {
99
            return $endpointType->getEndpoints($this->resource);
100
        }
101
102
        if ($this->endpointResourceType === EndpointResourceType::COLLECTION) {
103
            return $endpointType->getCollectionEndpoints();
104
        }
105
106
        if ($this->endpointResourceType === EndpointResourceType::MULTI) {
107
            return array_merge(
108
                $endpointType->getEndpoints($this->resource),
109
                $endpointType->getCollectionEndpoints()
110
            );
111
        }
112
113
        return [];
114
    }
115
116
    private function ensureCollectionEndpointsAreAutomaticallyMerged()
117
    {
118
        if ($this->endpointResourceType !== EndpointResourceType::ITEM) {
119
            return;
120
        }
121
122
        if (config('laravel-resource-endpoints.automatically_merge_endpoints') === false) {
123
            return;
124
        }
125
126
        if (is_null($this->resource) || $this->resource->exists === false) {
127
            $this->mergeCollectionEndpoints();
128
        }
129
    }
130
}
131