Completed
Push — master ( e79b43...d0162c )
by Ruben
01:28
created

EndpointResource::resolveControllerEndpoints()   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
13
final class EndpointResource extends JsonResource
14
{
15
    /** @var string */
16
    protected $endpointResourceType;
17
18
    /** @var \Illuminate\Support\Collection */
19
    private $endPointTypes;
20
21
    /** @var \Illuminate\Database\Eloquent\Model */
22
    private $model;
23
24
    public function __construct(Model $model = null, string $endpointResourceType = null)
25
    {
26
        parent::__construct($model);
27
28
        $this->endPointTypes = new Collection();
29
        $this->model = $model;
30
        $this->endpointResourceType = $endpointResourceType ?? EndpointResourceType::LOCAL;
31
    }
32
33
    public function addController(string $controller, $parameters = null): JsonResource
34
    {
35
        $this->endPointTypes->push(new ControllerEndpointType(
36
            $controller,
37
            $this->resolveProvidedParameters($parameters)
38
        ));
39
40
        return $this;
41
    }
42
43
    public function addAction(array $action, $parameters = null, string $httpVerb = null): JsonResource
44
    {
45
        $this->endPointTypes->push(new ActionEndpointType(
46
            $action,
47
            $this->resolveProvidedParameters($parameters),
48
            $httpVerb
49
        ));
50
51
        return $this;
52
    }
53
54
    public function toArray($request)
55
    {
56
        return $this->endPointTypes->mapWithKeys(function (EndPointType $endpointType) {
57
            if ($endpointType instanceof ControllerEndpointType) {
58
                return $this->resolveControllerEndpoints($endpointType);
59
            }
60
61
            return $endpointType->getEndpoints($this->model);
62
        });
63
    }
64
65
    private function resolveProvidedParameters($parameters = null)
66
    {
67
        $parameters = Arr::wrap($parameters);
68
69
        return count($parameters) === 0
70
            ? request()->route()->parameters()
71
            : $parameters;
72
    }
73
74
    private function resolveControllerEndpoints(ControllerEndpointType $endpointType): array
75
    {
76
        if ($this->endpointResourceType === EndpointResourceType::LOCAL) {
77
            return $endpointType->getGlobalEndpoints();
78
        }
79
80
        if ($this->endpointResourceType === EndpointResourceType::GLOBAL) {
81
            return $endpointType->getEndpoints($this->model);
82
        }
83
84
        if ($this->endpointResourceType === EndpointResourceType::MULTI) {
85
            return array_merge(
86
                $endpointType->getEndpoints($this->model),
87
                $endpointType->getGlobalEndpoints()
88
            );
89
        }
90
91
        return [];
92
    }
93
}
94