Failed Conditions
Push — master ( 45d676...509703 )
by Sébastien
05:56
created

InfoService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 62
rs 10
c 0
b 0
f 0
ccs 22
cts 26
cp 0.8462
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInfo() 0 17 4
A getSymfonyProcess() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Common\Assert\PathAssertionsTrait;
8
use Soluble\MediaTools\Common\Exception\FileNotFoundException;
9
use Soluble\MediaTools\Common\Process\ProcessFactory;
10
use Soluble\MediaTools\Common\Process\ProcessParamsInterface;
11
use Soluble\MediaTools\Video\Config\FFProbeConfigInterface;
12
use Soluble\MediaTools\Video\Exception\InfoExceptionInterface;
13
use Soluble\MediaTools\Video\Exception\InfoProcessExceptionInterface;
14
use Soluble\MediaTools\Video\Exception\MissingInputFileException;
15
use Soluble\MediaTools\Video\Exception\ProcessFailedException;
16
use Soluble\MediaTools\Video\Exception\RuntimeException;
17
use Symfony\Component\Process\Exception as SPException;
18
use Symfony\Component\Process\Process;
19
20
class InfoService implements InfoServiceInterface
21
{
22
    use PathAssertionsTrait;
23
24
    /** @var FFProbeConfigInterface */
25 2
    protected $ffprobeConfig;
26
27 2
    public function __construct(FFProbeConfigInterface $ffProbeConfig)
28 2
    {
29
        $this->ffprobeConfig = $ffProbeConfig;
30
    }
31
32
    /**
33
     * Return ready-to-run symfony process object that you can use
34
     * to `run()` or `start()` programmatically. Useful if you want to make
35
     * things your way...
36
     *
37
     * @see https://symfony.com/doc/current/components/process.html
38
     */
39 1
    public function getSymfonyProcess(string $inputFile, ?ProcessParamsInterface $processParams = null): Process
40
    {
41 1
        $ffprobeCmd = trim(sprintf(
42 1
            '%s %s %s',
43 1
            $this->ffprobeConfig->getBinary(),
44 1
            implode(' ', [
45 1
                '-v quiet',
46
                '-print_format json',
47
                '-show_format',
48
                '-show_streams',
49
            ]),
50 1
            sprintf('-i %s', escapeshellarg($inputFile))
51
        ));
52
53 1
        $pp = $processParams ?? $this->ffprobeConfig->getProcessParams();
54 1
55 1
        return (new ProcessFactory($ffprobeCmd, $pp))();
56 1
    }
57
58 1
    /**
59
     * @throws InfoExceptionInterface
60
     * @throws InfoProcessExceptionInterface
61
     * @throws ProcessFailedException
62
     * @throws MissingInputFileException
63
     * @throws RuntimeException
64
     */
65
    public function getInfo(string $file): Info
66
    {
67
        try {
68 2
            $this->ensureFileExists($file);
69
            $process = $this->getSymfonyProcess($file);
70
71 2
            $process->mustRun();
72 1
            $output = $process->getOutput();
73
        } catch (FileNotFoundException $e) {
74 1
            throw new MissingInputFileException($e->getMessage());
75 1
        } catch (SPException\ProcessFailedException | SPException\ProcessTimedOutException | SPException\ProcessSignaledException $e) {
76 1
            throw new ProcessFailedException($e->getProcess(), $e);
77 1
        } catch (SPException\RuntimeException $e) {
78
            throw new RuntimeException($e->getMessage());
79
        }
80
81
        return Info::createFromFFProbeJson($file, $output);
82
    }
83
}
84