Completed
Push — develop ( 15ec3c...8b62a8 )
by Baptiste
02:19
created

Dependency::exposed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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