Passed
Push — master ( 3ce531...7a6df5 )
by Kevin
02:21
created

ProcessTask   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 42
ccs 18
cts 19
cp 0.9474
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 2
A __construct() 0 13 3
A getContext() 0 5 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task;
4
5
use Symfony\Component\Process\Process;
6
use Zenstruck\ScheduleBundle\Schedule\Task;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class ProcessTask extends Task implements SelfRunningTask
12
{
13
    private $process;
14
15
    /**
16
     * @param string|Process $process
17
     */
18 12
    public function __construct($process)
19
    {
20 12
        if (!\class_exists(Process::class)) {
21
            throw new \LogicException(\sprintf('"symfony/process" is required to use "%s". Install with "composer require symfony/process".', self::class));
22
        }
23
24 12
        if (!$process instanceof Process) {
25 11
            $process = Process::fromShellCommandline($process);
26
        }
27
28 12
        $this->process = $process;
29
30 12
        parent::__construct($process->getCommandLine());
31 12
    }
32
33 3
    public function __invoke(): Result
34
    {
35 3
        $this->process->run();
36
37 3
        if ($this->process->isSuccessful()) {
38 2
            return Result::successful($this, $this->process->getOutput());
39
        }
40
41 1
        return Result::failure(
42 1
            $this,
43 1
            "Exit {$this->process->getExitCode()}: {$this->process->getExitCodeText()}",
44 1
            $this->process->getErrorOutput()
45
        );
46
    }
47
48 1
    public function getContext(): array
49
    {
50
        return [
51 1
            'Command Line' => $this->process->getCommandLine(),
52 1
            'Command Timeout' => $this->process->getTimeout(),
53
        ];
54
    }
55
}
56