Passed
Push — master ( 9478ec...26cf57 )
by Paweł
02:48
created

GameKernel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure;
6
7
use AardsGerds\Game\Infrastructure\Cli\RunGameCommand;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\Console\Application;
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
        $this->console->add(new RunGameCommand());
57 2
    }
58
}
59