Completed
Push — master ( e64811...19f81e )
by Freek
01:12
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\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
class EndpointResource extends JsonResource
14
{
15
    /** @var string */
16
    protected $endpointResourceType;
17
18
    /** @var \Illuminate\Support\Collection */
19
    protected $endPointTypes;
20
21
    /** @var \Illuminate\Database\Eloquent\Model */
22
    protected $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 mergeCollectionEndpoints() : JsonResource
55
    {
56
        $this->endpointResourceType = EndpointResourceType::MULTI;
57
58
        return $this;
59
    }
60
61
    public function toArray($request)
62
    {
63
        return $this->endPointTypes->mapWithKeys(function (EndPointType $endpointType) {
64
            if ($endpointType instanceof ControllerEndpointType) {
65
                return $this->resolveControllerEndpoints($endpointType);
66
            }
67
68
            return $endpointType->getEndpoints($this->model);
69
        });
70
    }
71
72
    protected function resolveProvidedParameters($parameters = null)
73
    {
74
        $parameters = Arr::wrap($parameters);
75
76
        return count($parameters) === 0
77
            ? request()->route()->parameters()
78
            : $parameters;
79
    }
80
81
    protected function resolveControllerEndpoints(ControllerEndpointType $endpointType): array
82
    {
83
        if ($this->endpointResourceType === EndpointResourceType::LOCAL) {
84
            return $endpointType->getEndpoints($this->model);
85
        }
86
87
        if ($this->endpointResourceType === EndpointResourceType::GLOBAL) {
88
            return $endpointType->getCollectionEndpoints();
89
        }
90
91
        if ($this->endpointResourceType === EndpointResourceType::MULTI) {
92
            return array_merge(
93
                $endpointType->getEndpoints($this->model),
94
                $endpointType->getCollectionEndpoints()
95
            );
96
        }
97
98
        return [];
99
    }
100
}
101