Factory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 49
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 3 1
A __construct() 0 4 1
A __toString() 0 3 1
A fromString() 0 9 2
A __invoke() 0 11 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service\Constructor;
5
6
use Innmind\Compose\{
7
    Definition\Service\Constructor,
8
    Exception\ValueNotSupported,
9
    Lazy,
10
    Compilation\Service\Constructor as CompiledConstructor,
11
    Compilation\Service\Constructor\Factory as CompiledFactory,
12
    Compilation\Service\Argument as CompiledArgument
13
};
14
use Innmind\Immutable\{
15
    Str,
16
    Sequence
17
};
18
19
final class Factory implements Constructor
20
{
21
    private $class;
22
    private $method;
23
24 4
    private function __construct(string $class, string $method)
25
    {
26 4
        $this->class = $class;
27 4
        $this->method = $method;
28 4
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 19
    public static function fromString(Str $value): Constructor
34
    {
35 19
        if (!$value->matches('~^\S+::\S+$~')) {
36 15
            throw new ValueNotSupported((string) $value);
37
        }
38
39 4
        [$class, $method] = $value->split('::');
40
41 4
        return new self((string) $class, (string) $method);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function __invoke(...$arguments): object
48
    {
49 2
        $arguments = Sequence::of(...$arguments)->map(static function($argument) {
50 2
            if ($argument instanceof Lazy) {
51 1
                return $argument->load();
52
            }
53
54 2
            return $argument;
55 2
        });
56
57 2
        return [$this->class, $this->method](...$arguments);
58
    }
59
60 1
    public function compile(CompiledArgument ...$arguments): CompiledConstructor
61
    {
62 1
        return new CompiledFactory($this->class, $this->method, ...$arguments);
63
    }
64
65 1
    public function __toString(): string
66
    {
67 1
        return sprintf('%s::%s', $this->class, $this->method);
68
    }
69
}
70