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

EndpointsGroup   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 53
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A controller() 0 12 2
A invokableController() 0 8 1
A action() 0 8 1
A endpointsGroup() 0 6 1
A getEndpointTypes() 0 4 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