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

BuilderFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A endpoint() 0 6 1
A nameFromClassName() 0 5 1
A call() 0 7 1
A repeat() 0 7 1
A reduce() 0 7 1
A many() 0 4 1
A checkSelf() 0 6 1
A input() 0 5 1
A output() 0 5 1
1
<?php
2
3
namespace hiapi\Core\Endpoint;
4
5
use Closure;
6
use hiapi\endpoints\Module\InOutControl\VO\Collection;
7
use hiapi\middlewares\CallableHandler;
8
use ReflectionClass;
9
use hiapi\Core\Endpoint\Middleware\ReduceHandler;
10
use hiapi\Core\Endpoint\Middleware\RepeatHandler;
11
use hiapi\endpoints\BuilderFactoryInterface;
12
use hiqdev\yii\compat\yii;
13
14
/**
15
 * Class BuilderFactory
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 */
19
class BuilderFactory implements BuilderFactoryInterface
20
{
21
    public function endpoint(string $className): EndpointBuilder
22
    {
23
        return (new EndpointBuilder())
24
            ->name($this->nameFromClassName($className))
25
            ->definedBy($className);
26
    }
27
28
    private function nameFromClassName(string $className): string
29
    {
30
        /** @noinspection PhpUnhandledExceptionInspection */
31
        return (new ReflectionClass($className))->getShortName();
32
    }
33
34
    public function call(string $className)
35
    {
36
        return [
37
            '__class' => CallableHandler::class,
38
            '__construct()' => [yii::referenceTo($className)],
39
        ];
40
    }
41
42
    public function repeat(string $className)
43
    {
44
        return [
45
            '__class' => RepeatHandler::class,
46
            '__construct()' => [yii::referenceTo($className)]
47
        ];
48
    }
49
50
    public function reduce(string $className)
51
    {
52
        return [
53
            '__class' => ReduceHandler::class,
54
            '__construct()' => [yii::referenceTo($className)]
55
        ];
56
    }
57
58
    public function many(string $className): Collection
59
    {
60
        return Collection::of($className);
61
    }
62
63
    // TODO: move to internal project
64
    public function checkSelf(): Closure
65
    {
66
        return static function ($command, $next) {
67
            return $next($command);
68
        };
69
    }
70
71
    public function input(array $inputOptions): array
72
    {
73
        // TODO: Take Tafid's Pact PR
74
        return $inputOptions;
75
    }
76
    public function output(array $inputOptions): array
77
    {
78
        // TODO: Take Tafid's Pact PR
79
        return $inputOptions;
80
    }
81
}
82