Completed
Push — master ( 32f082...893399 )
by Dmitry
12:23
created

Endpoint::getPermissions()   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 hiapi\Core\Endpoint;
4
5
use hiapi\endpoints\EndpointConfigurationInterface;
6
use hiapi\endpoints\Module\InOutControl\VO\Collection;
7
use hiapi\endpoints\Module\Multitenant\Tenant;
8
use Webmozart\Assert\Assert;
9
use hiapi\commands\BaseCommand;
10
11
/**
12
 * Class Endpoint
13
 *
14
 * @author Dmytro Naumenko <[email protected]>
15
 *
16
 * @psalm-suppress PropertyNotSetInConstructor
17
 * @psalm-immutable
18
 */
19
final class Endpoint
20
{
21
    public string $name;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
22
    public ?string $description;
23
24
    /** @psalm-var ?class-string */
25
    public ?string $definedBy;
26
    /** @psalm-var Tenant::CLI|Tenant::WEB */
27
    public int $tenantMask;
28
    /** @psalm-var Collection|class-string<BaseCommand> */
29
    public $inputType;
30
    /** @var \Closure[]|callable[] */
31
    public array $middlewares = [];
32
    /** @psalm-var list<string> */
33
    public array $permissions = [];
34
    /** @psalm-var Collection|class-string */
35
    public $returnType;
36
37
    /**
38
     * // TODO
39
     */
40
    public $examples;
41
42
    public static function fromConfig(EndpointConfigurationInterface $config): self
43
    {
44
        $self = new self();
45
46
        Assert::notEmpty($config['name'], 'Endpoint MUST have a name');
47
        $self->name = $config['name'];
48
        $self->definedBy = $config['definitionClassName'] ?? null;
49
        $self->permissions = !empty($config['permission']) ? [$config['permission']] : [];
50
        $self->tenantMask = $config['tenantMask'] ?? Tenant::CLI;
51
52
        Assert::notEmpty($config['inputType'], 'Endpoint input definition is required');
53
        $self->inputType = $config['inputType'];
54
55
        Assert::notEmpty($config['returnType'], 'Endpoint return definition is required');
56
        $self->returnType = $config['returnType'];
57
58
        Assert::isArray($config['middlewares'] ?? []);
59
        $self->middlewares = $config['middlewares'] ?? [];
60
61
        $self->examples = $config['examples'] ?? null;
62
        $self->description = $config['description'] ?? null;
63
64
        return $self;
65
    }
66
67
    private function __construct()
68
    {
69
    }
70
}
71