Completed
Push — master ( 2c64ed...ead60c )
by Ruben
07:32
created

EndpointsGroup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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