Completed
Push — master ( 9a342a...a162ae )
by Andrii
13:06
created

Endpoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 105
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromConfig() 0 23 1
A getTenantMask() 0 4 1
A getMiddlewares() 0 4 1
A getExamples() 0 4 1
A getName() 0 4 1
A getPermissions() 0 4 1
A getDefinedBy() 0 4 1
A getReturnType() 0 4 1
A getInputType() 0 4 1
1
<?php
2
3
namespace hiapi\Core\Endpoint;
4
5
use hiapi\endpoints\EndpointConfigurationInterface;
6
use hiapi\endpoints\Module\Multitenant\Tenant;
7
use Webmozart\Assert\Assert;
8
9
final class Endpoint
10
{
11
    /** @var string */
12
    private $name;
13
    /** @var int|null */
14
    private $tenantMask;
15
    /** @var \Closure[]|callable[] */
16
    private $middlewares;
17
    /**
18
     * // TODO: think
19
     */
20
    private $examples;
21
    private $permission;
22
    private $inputType;
23
    private $returnType;
24
    /**
25
     * @var string|null
26
     */
27
    private $definedBy;
28
29
    public static function fromConfig(EndpointConfigurationInterface $config)
30
    {
31
        $self = new self();
32
33
        Assert::notEmpty($config['name'], 'Endpoint MUST have a name');
34
        $self->name = $config['name'];
35
        $self->definedBy = $config['definitionClassName'] ?? null;
36
        $self->permission = $config['permission'] ?? null;
37
        $self->tenantMask = $config['tenantMask'] ?? 0x0;
38
39
        Assert::notEmpty($config['inputType'], 'Endpoint input definition is required');
40
        $self->inputType = $config['inputType'];
41
42
        Assert::notEmpty($config['returnType'], 'Endpoint return definition is required');
43
        $self->returnType = $config['returnType'];
44
45
        Assert::isArray($config['middlewares'] ?? []);
46
        $self->middlewares = $config['middlewares'] ?? [];
47
48
        $self->examples = $config['examples'] ?? null;
49
50
        return $self;
51
    }
52
53
    public function getTenantMask(): int
54
    {
55
        return $this->tenantMask ?? Tenant::CLI;
56
    }
57
58
    /**
59
     * @return string[]
60
     */
61
    public function getMiddlewares(): array
62
    {
63
        return $this->middlewares;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getExamples(): array
70
    {
71
        return $this->examples;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getName(): string
78
    {
79
        return $this->name;
80
    }
81
82
    /**
83
     * @return string[]
84
     */
85
    public function getPermissions(): array
86
    {
87
        return array_filter([$this->permission]);
88
    }
89
90
    /**
91
     * @return string|null
92
     */
93
    public function getDefinedBy(): ?string
94
    {
95
        return $this->definedBy;
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function getReturnType()
102
    {
103
        return $this->returnType;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getInputType()
110
    {
111
        return $this->inputType;
112
    }
113
}
114