Completed
Push — master ( 477c6b...8b3cf8 )
by Sébastien
04:39 queued 01:53
created

AbstractProcess   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 32
ccs 5
cts 15
cp 0.3333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A runCommand() 0 9 2
A ensureBinaryExists() 0 14 4
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
    public function runCommand(string $cmd): string
18
    {
19
        $process = new Process($cmd);
20
        try {
21
            $process->mustRun();
22
23
            return $process->getOutput();
24
        } catch (ProcessFailedException $e) {
25
            throw $e;
26
        }
27
    }
28
29 1
    public function ensureBinaryExists(): void
30
    {
31 1
        $binary = $this->getBinary();
32
        // Case of binary (no path given), we cannot tell
33 1
        if (basename($binary) === $binary) {
34 1
            $exists = true; // assume it exists
35
        } else {
36
            $exists = file_exists($binary) && is_executable($binary);
37
        }
38
39 1
        if (!$exists) {
40
            throw new MissingBinaryException(sprintf(
41
                'Missing ffprobe binary "%s"',
42
                $binary
43
            ));
44
        }
45 1
    }
46
}
47