Lines 54.54% 6 / 11
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 createForInstalledProject 100.00% 2 / 2 100.00% 1 / 1 2
 launchConsole 0.00% 0 / 5 0.00% 0 / 1 6
 loadEnvironmentFile 100.00% 3 / 3 100.00% 1 / 1 2
12final readonly class ConsoleLauncher
13{
14    private const string ENVIRONMENT_FILE = '/.env';
15
16    public function __construct(
17        public string $projectDir,
18        private ClassLoader $loader,
19    ) {}
20
21    public static function createForInstalledProject(ClassLoader $loader): self
22    {
23        $installPath = InstalledVersions::getRootPackage()['install_path'];
24
25        return new self(realpath($installPath) ?: $installPath, $loader);
26    }
27
28    public function launchConsole(): int
29    {
30        try {
31            $this->loadEnvironmentFile();
32        } catch (DotenvException $e) {
33            fwrite(\STDERR, \sprintf("Could not read %s: %s\n", $this->projectDir . self::ENVIRONMENT_FILE, $e->getMessage()));
34
35            return 1;
36        }
37
38        return new ConsoleKernel($this->projectDir, $this->loader->getPrefixesPsr4())->createApplication()->run();
39    }
40
41    /** @throws DotenvException */
42    public function loadEnvironmentFile(): void
43    {
44        $file = $this->projectDir . self::ENVIRONMENT_FILE;
45
46        if (is_file($file)) {
47            new Dotenv()->usePutenv()->load($file);
48        }
49    }
50}