Completed
Push — master ( d9d1e2...b73c31 )
by Ruben
01:14
created

LinkResource::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\ResourceLinks;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Spatie\ResourceLinks\LinkTypes\ControllerLinkType;
8
use Spatie\ResourceLinks\LinkTypes\LinkType;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Http\Resources\Json\JsonResource;
11
12
class LinkResource extends JsonResource
13
{
14
    /** @var string */
15
    private $linkResourceType;
16
17
    /** @var \Spatie\ResourceLinks\Links */
18
    private $linksGroup;
19
20
    public static function create(Model $model = null, string $linkResourceType = null): LinkResource
21
    {
22
        return new self($model, $linkResourceType);
23
    }
24
25
    public function __construct(Model $model = null, string $linkResourceType = null)
26
    {
27
        parent::__construct($model);
28
29
        $this->linkResourceType = $linkResourceType ?? LinkResourceType::ITEM;
30
        $this->linksGroup = new Links();
31
    }
32
33
    public function link($link, $parameters = null, $httpVerb = null): LinkResource
34
    {
35
        if ($link instanceof Closure) {
36
            $link($this->linksGroup);
37
38
            return $this;
39
        }
40
41
        if (is_array($link)) {
42
            $this->linksGroup
43
                ->action($link)
44
                ->httpVerb($httpVerb)
45
                ->parameters(Arr::wrap($parameters));
46
47
            return $this;
48
        }
49
50
        if (is_string($link) && method_exists($link, '__invoke')) {
51
            $this->linksGroup
52
                ->invokableController($link)
53
                ->parameters(Arr::wrap($parameters));
54
55
            return $this;
56
        }
57
58
        if (is_string($link)) {
59
            $this->linksGroup
60
                ->controller($link)
61
                ->parameters(Arr::wrap($parameters));
62
63
            return $this;
64
        }
65
66
        return $this;
67
    }
68
69
    public function action(array $action, $parameters = null, $httpVerb = null): LinkResource
70
    {
71
        return $this->link($action, $parameters, $httpVerb);
72
    }
73
74
    public function controller(string $controller, $parameters = null): LinkResource
75
    {
76
        return $this->link($controller, $parameters);
77
    }
78
79
    public function links(Closure $closure): LinkResource
80
    {
81
        return $this->link($closure);
82
    }
83
84
    public function withCollectionLinks(): LinkResource
85
    {
86
        $this->linkResourceType = LinkResourceType::MULTI;
87
88
        return $this;
89
    }
90
91
    public function toArray($request)
92
    {
93
        return $this->linksGroup
94
            ->getLinkTypes()
95
            ->mapWithKeys(function (LinkType $linkType) use ($request) {
96
                $linkType->parameters($request->route()->parameters());
97
98
                if ($linkType instanceof ControllerLinkType) {
99
                    return $this->resolveLinksFromControllerLinkType($linkType);
100
                }
101
102
                return $linkType->getLinks($this->resource);
103
            });
104
    }
105
106
    private function resolveLinksFromControllerLinkType(ControllerLinkType $controllerLinkType): array
107
    {
108
        if ($this->linkResourceType === LinkResourceType::ITEM) {
109
            return $controllerLinkType->getLinks($this->resource);
110
        }
111
112
        if ($this->linkResourceType === LinkResourceType::COLLECTION) {
113
            return $controllerLinkType->getCollectionLinks();
114
        }
115
116
        if ($this->linkResourceType === LinkResourceType::MULTI) {
117
            return array_merge(
118
                $controllerLinkType->getLinks($this->resource),
119
                $controllerLinkType->getCollectionLinks()
120
            );
121
        }
122
123
        return [];
124
    }
125
}
126