Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.00% covered (danger)
35.00%
7 / 20
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SymfonyUtils
35.00% covered (danger)
35.00%
7 / 20
0.00% covered (danger)
0.00%
0 / 3
25.58
0.00% covered (danger)
0.00%
0 / 1
 callCommand
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 getApplication
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 fromEnv
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Olz\Utils;
4
5use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Command\Command;
7use Symfony\Component\Console\Input\InputInterface;
8use Symfony\Component\Console\Output\OutputInterface;
9
10class SymfonyUtils {
11    use WithUtilsTrait;
12
13    protected static ?Application $application = null;
14
15    public function callCommand(
16        string $command_name,
17        InputInterface $input,
18        OutputInterface $output,
19    ): void {
20        $application = $this->getApplication();
21        $this->generalUtils()->checkNotNull($application, "No application present");
22        $application->setAutoExit(false);
23        $command = $application->find($command_name);
24        $return_code = $command->run($input, $output);
25        if ($return_code === Command::FAILURE) {
26            throw new \Exception("Command {$command_name} failed.");
27        }
28        if ($return_code === Command::INVALID) {
29            throw new \Exception("Command {$command_name} called with invalid arguments.");
30        }
31        if ($return_code !== Command::SUCCESS) {
32            throw new \Exception("Command {$command_name} failed with unknown code: {$return_code}.");
33        }
34    }
35
36    public function getApplication(): ?Application {
37        if (self::$application !== null) {
38            return self::$application;
39        }
40
41        global $kernel;
42
43        if (!$kernel) {
44            return null;
45        }
46
47        $application = new Application($kernel);
48        self::$application = $application;
49        return $application;
50    }
51
52    public static function fromEnv(): self {
53        return new self();
54    }
55}