Completed
Push — master ( 854f9f...ee186a )
by Ruben
01:12
created

Links::invokableController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\ResourceLinks;
4
5
use Illuminate\Support\Collection;
6
use Spatie\ResourceLinks\LinkTypes\ActionLinkType;
7
use Spatie\ResourceLinks\LinkTypes\ControllerLinkType;
8
use Spatie\ResourceLinks\LinkTypes\InvokableControllerLinkType;
9
10
class Links
11
{
12
    /** @var \Illuminate\Support\Collection */
13
    private $linkTypes;
14
15
    public function __construct()
16
    {
17
        $this->linkTypes = new Collection();
18
    }
19
20
    /**
21
     * @param string $controller
22
     *
23
     * @return \Spatie\ResourceLinks\LinkTypes\ControllerLinkType|\Spatie\ResourceLinks\LinkTypes\ActionLinkType
24
     */
25
    public function controller(string $controller)
26
    {
27
        $linkType = method_exists($controller, '__invoke')
28
            ? ActionLinkType::make([$controller])
29
            : ControllerLinkType::make($controller);
30
31
        $this->linkTypes[] = $linkType;
32
33
        return $linkType;
34
    }
35
36
    public function action(array $action): ActionLinkType
37
    {
38
        $actionLinkType = ActionLinkType::make($action);
39
40
        $this->linkTypes[] = $actionLinkType;
41
42
        return $actionLinkType;
43
    }
44
45
    public function links(Links $links)
46
    {
47
        $this->linkTypes = $this->linkTypes->merge(
48
            $links->getLinkTypes()
49
        );
50
    }
51
52
    public function getLinkTypes(): Collection
53
    {
54
        return $this->linkTypes;
55
    }
56
}
57