Passed
Push — develop ( 7d2249...4ccadf )
by Baptiste
02:09
created

Factory::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
};
11
use Innmind\Immutable\{
12
    Str,
13
    Sequence
14
};
15
16
final class Factory implements Constructor
17
{
18
    private $class;
19
    private $method;
20
21 3
    private function __construct(string $class, string $method)
22
    {
23 3
        $this->class = $class;
24 3
        $this->method = $method;
25 3
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 13
    public static function fromString(Str $value): Constructor
31
    {
32 13
        if (!$value->matches('~^\S+::\S+$~')) {
33 10
            throw new ValueNotSupported((string) $value);
34
        }
35
36 3
        [$class, $method] = $value->split('::');
37
38 3
        return new self((string) $class, (string) $method);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function __invoke(...$arguments): object
45
    {
46 2
        $arguments = Sequence::of(...$arguments)->map(static function($argument) {
47 2
            if ($argument instanceof Lazy) {
48 1
                return $argument->load();
49
            }
50
51 2
            return $argument;
52 2
        });
53
54 2
        return [$this->class, $this->method](...$arguments);
55
    }
56
57 1
    public function __toString(): string
58
    {
59 1
        return sprintf('%s::%s', $this->class, $this->method);
60
    }
61
}
62