Services::accessible()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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