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

Services::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 48
ccs 15
cts 15
cp 1
crap 1
rs 9.125
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
    Services as Container,
8
    Definition
9
};
10
use Innmind\Immutable\{
11
    SetInterface,
12
    Set,
13
    Str
14
};
15
16
final class Services
17
{
18
    private $services;
19
    private $arguments;
20
    private $dependencies;
21
22 5
    public function __construct(Container $services)
23
    {
24 5
        $this->services = $services
25 5
            ->all()
26 5
            ->filter(static function(Definition\Service $service): bool {
27 4
                return !$service->decorates();
28 5
            })
29 5
            ->reduce(
30 5
                Set::of(Service::class),
31 5
                static function(Set $services, Definition\Service $service): Set {
32 4
                    return $services->add($service->compile());
33 5
                }
34
            );
35 5
        $this->arguments = $services->arguments()->compile();
36 5
        $this->dependencies = $services->dependencies()->compile();
37 5
    }
38
39 4
    public function __toString(): string
40
    {
41
        $methods = $this
42 4
            ->services
43 4
            ->join("\n\n")
44 4
            ->append("\n\n")
45 4
            ->append((string) $this->arguments)
46 4
            ->append("\n\n")
47 4
            ->append($this->dependencies->exposed());
48 4
        $switchGet = $this->generateSwitchGet();
49 4
        $switchHas = $this->generateSwitchHas();
50
51
        return <<<PHP
52
new class(\$arguments) implements ContainerInterface {
53
    private \$arguments;
54
55
    // Dependencies
56 4
{$this->dependencies->properties()}
57
58
    // Services instances
59 4
{$this->properties()}
60
61
    public function __construct(MapInterface \$arguments)
62
    {
63
        \$this->arguments = \$arguments;
64 4
{$this->dependencies}
65
    }
66
67
    public function get(\$id): object
68
    {
69
        switch (\$id) {
70 4
$switchGet
71
        }
72
73
        throw new NotFound(\$id);
74
    }
75
76
    public function has(\$id): bool
77
    {
78
        switch (\$id) {
79 4
$switchHas
80
                return true;
81
        }
82
83
        return false;
84
    }
85
86 4
    $methods
87
};
88
PHP;
89
    }
90
91
    private function accessible(): SetInterface
92
    {
93 4
        return $this->services->filter(static function(Service $service): bool {
94 4
            return $service->accessible();
95 4
        });
96
    }
97
98 4
    private function properties(): Str
99
    {
100
        return $this
101 4
            ->services
102 4
            ->reduce(
103 4
                Set::of(Str::class),
104 4
                static function(Set $properties, Service $service): Set {
105 4
                    return $properties->add(
106 4
                        Str::of((string) $service->property())
107
                    );
108 4
                }
109
            )
110 4
            ->map(static function(Str $property): Str {
111
                return $property
112 4
                    ->prepend('    private $')
113 4
                    ->append(';');
114 4
            })
115 4
            ->join("\n");
116
    }
117
118 4
    private function generateSwitchGet(): Str
119
    {
120
        return $this
121 4
            ->accessible()
122 4
            ->reduce(
123 4
                Set::of('string'),
124 4
                static function(Set $accessible, Service $service): Set {
125
                    $code = <<<PHP
126 4
            case '{$service->name()}':
127 4
                return \$this->{$service->method()}();
128
PHP;
129
130 4
                    return $accessible->add($code);
131 4
                }
132
            )
133 4
            ->join("\n");
134
    }
135
136 4
    private function generateSwitchHas(): Str
137
    {
138
        return $this
139 4
            ->accessible()
140 4
            ->reduce(
141 4
                Set::of('string'),
142 4
                static function(Set $accessible, Service $service): Set {
143
                    $code = <<<PHP
144 4
            case '{$service->name()}':
145
PHP;
146
147 4
                    return $accessible->add($code);
148 4
                }
149
            )
150 4
            ->join("\n");
151
    }
152
}
153