Completed
Push — master ( ab46c1...42a3a5 )
by Ruben
02:07
created

resolveEndpointsFromMultiEndpointType()   A

Complexity

Conditions 4
Paths 4

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 4
nc 4
nop 1
1
<?php
2
3
namespace Spatie\LaravelEndpointResources;
4
5
use Illuminate\Support\Arr;
6
use Spatie\LaravelEndpointResources\EndpointTypes\ActionEndpointType;
7
use Spatie\LaravelEndpointResources\EndpointTypes\ControllerEndpointType;
8
use Spatie\LaravelEndpointResources\EndpointTypes\EndpointType;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Http\Resources\Json\JsonResource;
11
use Illuminate\Support\Collection;
12
use Spatie\LaravelEndpointResources\EndpointTypes\InvokableControllerEndpointType;
13
14
class EndpointResource extends JsonResource
15
{
16
    /** @var \Illuminate\Database\Eloquent\Model */
17
    protected $model;
18
19
    /** @var string */
20
    protected $endpointResourceType;
21
22
    /** @var \Illuminate\Support\Collection */
23
    protected $endPointTypes;
24
25
    public function __construct(Model $model = null, string $endpointResourceType = null)
26
    {
27
        parent::__construct($model);
28
29
        $this->model = $model;
30
        $this->endpointResourceType = $endpointResourceType ?? EndpointResourceType::ITEM;
31
        $this->endPointTypes = new Collection();
32
    }
33
34
    public function addController(string $controller, $parameters = null): JsonResource
35
    {
36
        if (method_exists($controller, '__invoke')) {
37
            return $this->addInvokableController($controller, $parameters);
38
        }
39
40
        $this->endPointTypes->push(new ControllerEndpointType(
41
            $controller,
42
            $this->resolveProvidedParameters($parameters)
43
        ));
44
45
        return $this;
46
    }
47
48
    public function addAction(array $action, $parameters = null, string $httpVerb = null): JsonResource
49
    {
50
        $this->endPointTypes->push(new ActionEndpointType(
51
            $action,
52
            $this->resolveProvidedParameters($parameters),
53
            $httpVerb
54
        ));
55
56
        return $this;
57
    }
58
59
    public function addInvokableController(string $controller, $parameters = null) : JsonResource
60
    {
61
        $this->endPointTypes->push(new InvokableControllerEndpointType(
62
            $controller,
63
            $this->resolveProvidedParameters($parameters)
64
        ));
65
66
        return $this;
67
    }
68
69
    public function mergeCollectionEndpoints(): JsonResource
70
    {
71
        $this->endpointResourceType = EndpointResourceType::MULTI;
72
73
        return $this;
74
    }
75
76
    public function toArray($request)
77
    {
78
        return $this->endPointTypes->mapWithKeys(function (EndPointType $endpointType) {
79
            if ($endpointType instanceof MultiEndpointType) {
80
                return $this->resolveEndpointsFromMultiEndpointType($endpointType);
81
            }
82
83
            return $endpointType->getEndpoints($this->model);
84
        });
85
    }
86
87
    protected function resolveProvidedParameters($parameters = null): array
88
    {
89
        $parameters = Arr::wrap($parameters);
90
91
        return count($parameters) === 0
92
            ? request()->route()->parameters()
93
            : $parameters;
94
    }
95
96
    protected function resolveEndpointsFromMultiEndpointType(MultiEndpointType $multiEndpointType): array
97
    {
98
        if ($this->endpointResourceType === EndpointResourceType::ITEM) {
99
            return $multiEndpointType->getEndpoints($this->model);
100
        }
101
102
        if ($this->endpointResourceType === EndpointResourceType::COLLECTION) {
103
            return $multiEndpointType->getCollectionEndpoints();
104
        }
105
106
        if ($this->endpointResourceType === EndpointResourceType::MULTI) {
107
            return array_merge(
108
                $multiEndpointType->getEndpoints($this->model),
109
                $multiEndpointType->getCollectionEndpoints()
110
            );
111
        }
112
113
        return [];
114
    }
115
}
116