Passed
Push — master ( 18ad97...6be5f6 )
by Paweł
03:10
created

GameKernel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 2
b 0
f 0
dl 0
loc 50
ccs 23
cts 26
cp 0.8846
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpConsole() 0 9 2
A setUpContainer() 0 14 1
A runConsole() 0 3 1
A getContainer() 0 3 1
A __construct() 0 5 1
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
    public const ENV_PROD = 'prod';
16
    public const ENV_TEST = 'test';
17
18
    private ContainerBuilder $container;
19
    private Application $console;
20
21 2
    public function __construct(
22
        private string $environment,
23
    ) {
24 2
        $this->setUpContainer();
25 2
        $this->setUpConsole();
26 2
    }
27
28
    public function runConsole(): void
29
    {
30
        $this->console->run();
31
    }
32
33 2
    public function getContainer(): ContainerBuilder
34
    {
35 2
        return $this->container;
36
    }
37
38 2
    private function setUpContainer(): void
39
    {
40 2
        $this->container = new ContainerBuilder();
41 2
        $projectDir = dirname(__DIR__, 2);
42 2
        $loader = new YamlFileLoader($this->container, new FileLocator("{$projectDir}/config"));
43
44 2
        $loader->load(match ($this->environment) {
45 2
            self::ENV_PROD => 'services.yaml',
46 2
            self::ENV_TEST => 'services_test.yaml',
47 2
            default => throw GameKernelException::unknownEnvironment($this->environment),
48
        });
49
50 2
        $this->container->setParameter('kernel.project_dir', $projectDir);
51 2
        $this->container->compile();
52 2
    }
53
54 2
    private function setUpConsole(): void
55
    {
56 2
        $this->console = new Application();
57 2
        $commands = $this->container->findTaggedServiceIds('console.command');
58
59 2
        foreach ($commands as $commandId => $tags) {
60
            /** @var Command $command */
61 2
            $command = $this->container->get($commandId);
62 2
            $this->console->add($command);
63
        }
64 2
    }
65
}
66