Passed
Push — master ( db43e8...42566f )
by Paweł
02:54
created

GameKernel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
c 2
b 0
f 0
dl 0
loc 49
ccs 23
cts 27
cp 0.8519
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpContainer() 0 16 1
A runConsole() 0 3 1
A getContainer() 0 3 1
A __construct() 0 5 1
A setUpConsole() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure;
6
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
13
final class GameKernel
14
{
15
    private ContainerBuilder $container;
16
    private Application $console;
17
18 2
    public function __construct(
19
        private string $environment,
20
    ) {
21 2
        $this->setUpContainer();
22 2
        $this->setUpConsole();
23 2
    }
24
25
    public function runConsole(): void
26
    {
27
        $this->console->run();
28
    }
29
30 2
    public function getContainer(): ContainerBuilder
31
    {
32 2
        return $this->container;
33
    }
34
35 2
    private function setUpContainer(): void
36
    {
37 2
        $this->container = new ContainerBuilder();
38 2
        $loader = new YamlFileLoader(
39 2
            $this->container,
40 2
            new FileLocator(dirname(__DIR__, 2) . '/config'),
41
        );
42
43 2
        $loader->load(match ($this->environment) {
44 2
            'test' => 'services_test.yaml',
45
            'prod' => 'services.yaml',
46 2
            default => throw GameKernelException::unknownEnvironment($this->environment),
47
        });
48
49 2
        $this->container->setParameter('kernel.project_dir', dirname(__DIR__, 2));
50 2
        $this->container->compile();
51 2
    }
52
53 2
    private function setUpConsole(): void
54
    {
55 2
        $this->console = new Application();
56 2
        $commands = $this->container->findTaggedServiceIds('console.command');
57
58 2
        foreach ($commands as $commandId => $tags) {
59
            /** @var Command $command */
60 2
            $command = $this->container->get($commandId);
61 2
            $this->console->add($command);
62
        }
63 2
    }
64
}
65