Passed
Push — master ( c9a7c7...1fb46e )
by Jakub
01:37
created

php$1 ➔ createInstallToolsUseCase()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Cli;
4
5
use Psr\Container\ContainerExceptionInterface;
6
use Psr\Container\ContainerInterface;
7
use Psr\Container\NotFoundExceptionInterface;
8
use RuntimeException;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Zalas\Toolbox\Cli\Command\InstallCommand;
12
use Zalas\Toolbox\Cli\Command\ListCommand;
13
use Zalas\Toolbox\Cli\Command\TestCommand;
14
use Zalas\Toolbox\Cli\Runner\DryRunner;
15
use Zalas\Toolbox\Cli\Runner\LazyRunner;
16
use Zalas\Toolbox\Json\JsonTools;
17
use Zalas\Toolbox\Runner\PassthruRunner;
18
use Zalas\Toolbox\Runner\Runner;
19
use Zalas\Toolbox\Tool\Tools;
20
use Zalas\Toolbox\UseCase\InstallTools;
21
use Zalas\Toolbox\UseCase\ListTools;
22
use Zalas\Toolbox\UseCase\TestTools;
23
24
class ServiceContainer implements ContainerInterface
25
{
26
    private $services = [
27
        InstallCommand::class => 'createInstallCommand',
28
        ListCommand::class => 'createListCommand',
29
        TestCommand::class => 'createTestCommand',
30
        Runner::class => 'createRunner',
31
        DryRunner::class => 'createDryRunner',
32
        PassthruRunner::class => 'createPassthruRunner',
33
        InstallTools::class => 'createInstallToolsUseCase',
34
        ListTools::class => 'createListToolsUseCase',
35
        TestTools::class => 'createTestToolsUseCase',
36
        Tools::class => 'createTools',
37
    ];
38
39
    private $runtimeServices = [
40
        InputInterface::class => null,
41
        OutputInterface::class => null,
42
    ];
43
44 15
    public function set(string $id, /*object */$service): void
45
    {
46 15
        if (!\array_key_exists($id, $this->runtimeServices)) {
47
            throw new class(\sprintf('The "%s" runtime service is not expected.', $id)) extends RuntimeException implements ContainerExceptionInterface {
48
            };
49
        }
50
51 15
        $this->runtimeServices[$id] = $service;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 21
    public function get($id)
58
    {
59 21
        if (isset($this->runtimeServices[$id])) {
60 4
            return $this->runtimeServices[$id];
61
        }
62
63 20
        if (isset($this->services[$id])) {
64 18
            return \call_user_func([$this, $this->services[$id]]);
65
        }
66
67
        throw new class(\sprintf('The "%s" service is not registered in the service container.', $id)) extends RuntimeException implements NotFoundExceptionInterface {
68
        };
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 21
    public function has($id)
75
    {
76 21
        return isset($this->services[$id]) || isset($this->runtimeServices[$id]);
77
    }
78
79 6
    private function createInstallCommand(): InstallCommand
80
    {
81 6
        return new InstallCommand($this->get(InstallTools::class), $this->get(Runner::class));
82
    }
83
84 4
    private function createListCommand(): ListCommand
85
    {
86 4
        return new ListCommand($this->get(ListTools::class));
87
    }
88
89 5
    private function createTestCommand(): TestCommand
90
    {
91 5
        return new TestCommand($this->get(TestTools::class), $this->get(Runner::class));
92
    }
93
94 4
    private function createRunner(): Runner
95
    {
96 4
        return new LazyRunner($this);
97
    }
98
99 1
    private function createPassthruRunner(): Runner
100
    {
101 1
        return new PassthruRunner();
102
    }
103
104 2
    private function createDryRunner(): Runner
105
    {
106 2
        return new DryRunner($this->get(OutputInterface::class));
107
    }
108
109 2
    private function createInstallToolsUseCase(): InstallTools
110
    {
111 2
        return new InstallTools($this->get(Tools::class));
112
    }
113
114 2
    private function createListToolsUseCase(): ListTools
115
    {
116 2
        return new ListTools($this->get(Tools::class));
117
    }
118
119 1
    private function createTestToolsUseCase(): TestTools
120
    {
121 1
        return new TestTools($this->get(Tools::class));
122
    }
123
124 5
    private function createTools(): Tools
125
    {
126
        return new JsonTools(function (): array {
127 2
            return $this->get(InputInterface::class)->getOption('tools');
128 5
        });
129
    }
130
}
131