Dependency::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Compilation;
5
6
use Innmind\Compose\{
7
    Definition\Name,
8
    Definition\Dependency\Parameter,
9
    Services as Container,
10
};
11
use Innmind\Immutable\{
12
    Stream,
13
    Sequence
14
};
15
16
final class Dependency
17
{
18
    private $name;
19
    private $services;
20
    private $parameters;
21
22 11
    public function __construct(
23
        Name $name,
24
        Container $services,
25
        Parameter ...$parameters
26
    ) {
27 11
        $this->name = $name;
28 11
        $this->services = $services;
29 11
        $this->parameters = Stream::of(Parameter::class, ...$parameters);
30 11
    }
31
32 6
    public function name(): Name
33
    {
34 6
        return $this->name;
35
    }
36
37 6
    public function exposed(): string
38
    {
39
        return (string) $this
40 6
            ->services
41 6
            ->exposed()
42 6
            ->keys()
43 6
            ->reduce(
44 6
                new Sequence,
45 6
                function(Sequence $methods, Name $service): Sequence {
46 6
                    $method = new MethodName($this->name->add($service));
47
48 6
                    return $methods->add(<<<PHP
49 6
    private function $method()
50
    {
51 6
        return \$this->{$this->name}->get('$service');
52
    }
53
PHP
54
                    );
55 6
                }
56
            )
57 6
            ->join("\n\n");
58
    }
59
60 6
    public function __toString(): string
61
    {
62 6
        $container = $this->services->compile();
63 6
        $property = new PropertyName($this->name);
64
65
        return <<<PHP
66 6
        \$arguments = (new \\Innmind\\Immutable\\Map('string', 'mixed')){$this->parameters()};
67 6
        \$this->$property = $container
68
PHP;
69
    }
70
71 6
    private function parameters(): string
72
    {
73 6
        if ($this->parameters->size() === 0) {
74 1
            return '';
75
        }
76
77
        return (string) $this
78 5
            ->parameters
79 5
            ->reduce(
80 5
                new Sequence,
81 5
                static function(Sequence $parameters, Parameter $parameter): Sequence {
82 5
                    return $parameters->add($parameter->compile());
83 5
                }
84
            )
85 5
            ->join("\n")
86 5
            ->prepend("\n");
87
    }
88
}
89