Completed
Push — master ( df43f8...12407a )
by Ruben
01:27
created

EndpointsGroup::action()   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\LaravelEndpointResources;
4
5
use Illuminate\Support\Collection;
6
use Spatie\LaravelEndpointResources\EndpointTypes\ActionEndpointType;
7
use Spatie\LaravelEndpointResources\EndpointTypes\ControllerEndpointType;
8
use Spatie\LaravelEndpointResources\EndpointTypes\InvokableControllerEndpointType;
9
use Spatie\LaravelEndpointResources\EndpointTypes\MultiEndpointType;
10
11
class EndpointsGroup
12
{
13
    /** @var \Illuminate\Support\Collection */
14
    private $endpointTypes;
15
16
    public function __construct()
17
    {
18
        $this->endpointTypes = new Collection();
19
    }
20
21
    public function controller(string $controller): MultiEndpointType
22
    {
23
        if (method_exists($controller, '__invoke')) {
24
            return $this->invokableController($controller);
25
        }
26
27
        $controllerEndpointType = ControllerEndpointType::make($controller);
28
29
        $this->endpointTypes[] = $controllerEndpointType;
30
31
        return $controllerEndpointType;
32
    }
33
34
    public function invokableController(string $controller): MultiEndpointType
35
    {
36
        $invokableControllerEndpointType = InvokableControllerEndpointType::make($controller);
37
38
        $this->endpointTypes[] = $invokableControllerEndpointType;
39
40
        return $invokableControllerEndpointType;
41
    }
42
43
    public function action(array $action): ActionEndpointType
44
    {
45
        $actionEndpointType = ActionEndpointType::make($action);
46
47
        $this->endpointTypes[] = $actionEndpointType;
48
49
        return $actionEndpointType;
50
    }
51
52
    public function endpointsGroup(EndpointsGroup $endpointsGroup)
53
    {
54
        $this->endpointTypes = $this->endpointTypes->merge(
55
            $endpointsGroup->getEndpointTypes()
56
        );
57
    }
58
59
    public function getEndpointTypes(): Collection
60
    {
61
        return $this->endpointTypes;
62
    }
63
}
64