Completed
Push — master ( 711a5f...8a8686 )
by Ruben
03:06
created

EndpointResource::mergeCollectionEndpoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
        } elseif (is_array($endpoint)) {
38
            $this->endpointsGroup
39
                ->action($endpoint)
40
                ->httpVerb($httpVerb)
41
                ->parameters(Arr::wrap($parameters));
42
        } elseif (is_string($endpoint)) {
43
            $this->endpointsGroup
44
                ->controller($endpoint)
45
                ->parameters(Arr::wrap($parameters));
46
        }
47
48
        return $this;
49
    }
50
51
    public function mergeCollectionEndpoints(): EndpointResource
52
    {
53
        $this->endpointResourceType = EndpointResourceType::MULTI;
54
55
        return $this;
56
    }
57
58
    public function toArray($request)
59
    {
60
        $this->ensureCollectionEndpointsAreAutomaticallyMerged();
61
62
        return $this->endpointsGroup
63
            ->getEndpointTypes()
64
            ->map(function (EndpointType $endpointType) use ($request) {
65
                return $endpointType->hasParameters() === false
66
                    ? $endpointType->parameters($request->route()->parameters())
67
                    : $endpointType;
68
            })
69
            ->mapWithKeys(function (EndPointType $endpointType) {
70
                if ($endpointType instanceof ControllerEndpointType) {
71
                    return $this->resolveEndpointsFromControllerEndpointType($endpointType);
72
                }
73
74
                return $endpointType->getEndpoints($this->resource);
75
            });
76
    }
77
78
    private function resolveEndpointsFromControllerEndpointType(ControllerEndpointType $endpointType): array
79
    {
80
        if ($this->endpointResourceType === EndpointResourceType::ITEM) {
81
            return $endpointType->getEndpoints($this->resource);
82
        }
83
84
        if ($this->endpointResourceType === EndpointResourceType::COLLECTION) {
85
            return $endpointType->getCollectionEndpoints();
86
        }
87
88
        if ($this->endpointResourceType === EndpointResourceType::MULTI) {
89
            return array_merge(
90
                $endpointType->getEndpoints($this->resource),
91
                $endpointType->getCollectionEndpoints()
92
            );
93
        }
94
95
        return [];
96
    }
97
98
    private function ensureCollectionEndpointsAreAutomaticallyMerged()
99
    {
100
        if ($this->endpointResourceType !== EndpointResourceType::ITEM) {
101
            return;
102
        }
103
104
        if (config('laravel-resource-endpoints.automatically-merge-endpoints') === false) {
105
            return;
106
        }
107
108
        if (is_null($this->resource) || $this->resource->exists === false) {
109
            $this->mergeCollectionEndpoints();
110
        }
111
    }
112
}
113