CommandTaskRunner::getFailureMessage()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 1
cp 0
rs 10
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner;
4
5
use Symfony\Component\Console\Application;
6
use Symfony\Component\Console\Output\BufferedOutput;
7
use Symfony\Component\Process\Process;
8
use Zenstruck\ScheduleBundle\Schedule\Task;
9
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
10
use Zenstruck\ScheduleBundle\Schedule\Task\Result;
11
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class CommandTaskRunner implements TaskRunner
17
{
18
    /** @var Application */
19
    private $application;
20 4
21
    public function __construct(Application $application)
22 4
    {
23 4
        $this->application = $application;
24
    }
25
26
    /**
27
     * @param CommandTask $task
28 3
     */
29
    public function __invoke(Task $task): Result
30 3
    {
31 3
        $shellVerbosityResetter = new ShellVerbosityResetter();
32 3
        $output = new BufferedOutput();
33
        $this->application->setCatchExceptions(false);
34
        $this->application->setAutoExit(false);
35 3
36 1
        try {
37 1
            $exitCode = $this->application->run($task->createCommandInput($this->application), $output);
0 ignored issues
show
Bug introduced by
The method createCommandInput() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle\Schedule\Task\CommandTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
            $exitCode = $this->application->run($task->/** @scrutinizer ignore-call */ createCommandInput($this->application), $output);
Loading history...
38
        } catch (\Throwable $e) {
39
            return Result::exception($task, $e, $output->fetch());
40 2
        } finally {
41 1
            $shellVerbosityResetter->reset();
42
        }
43
44 1
        if (0 === $exitCode) {
45
            return Result::successful($task, $output->fetch());
46
        }
47 1
48
        return Result::failure($task, "Exit {$exitCode}: {$this->getFailureMessage($exitCode)}", $output->fetch());
49 1
    }
50
51
    public function supports(Task $task): bool
52 1
    {
53
        return $task instanceof CommandTask;
54 1
    }
55 1
56
    private function getFailureMessage(int $exitCode): string
57
    {
58
        if (\class_exists(Process::class) && isset(Process::$exitCodes[$exitCode])) {
59
            return Process::$exitCodes[$exitCode];
60
        }
61
62
        return 'Unknown error';
63
    }
64
}
65