Passed
Push — master ( 00ec2b...220eca )
by Sébastien
03:08 queued 47s
created

AbstractProcess::runCommand()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
ccs 4
cts 6
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 2.1481
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Process;
6
7
use Soluble\MediaTools\Exception\MissingBinaryException;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Process;
10
11
abstract class AbstractProcess
12
{
13
    abstract public function buildCommand(array $arguments): string;
14
15
    abstract public function getBinary(): string;
16
17 1
    public function runCommand(string $cmd): string
18
    {
19 1
        $process = new Process($cmd);
20
        try {
21 1
            $process->mustRun();
22
23 1
            return $process->getOutput();
24
        } catch (ProcessFailedException $e) {
25
            throw $e;
26
        }
27
    }
28
29 5
    public function ensureBinaryExists(): void
30
    {
31 5
        $binary = $this->getBinary();
32
        // Case of binary (no path given), we cannot tell
33 5
        if (basename($binary) === $binary) {
34
            $exists = true; // assume it exists
35
        } else {
36 5
            $exists = file_exists($binary) && is_executable($binary);
37
        }
38
39 5
        if (!$exists) {
40
            throw new MissingBinaryException(sprintf(
41
                'Missing ffprobe binary "%s"',
42
                $binary
43
            ));
44
        }
45 5
    }
46
}
47