MediaInfoCommandRunner::start()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Mhor\MediaInfo\Runner;
4
5
use Symfony\Component\Process\Process;
6
7
class MediaInfoCommandRunner
8
{
9
    const MEDIAINFO_COMMAND = 'mediainfo';
10
    const MEDIAINFO_OLDXML_OUTPUT_ARGUMENT = '--OUTPUT=OLDXML';
11
    const MEDIAINFO_XML_OUTPUT_ARGUMENT = '--OUTPUT=XML';
12
    const MEDIAINFO_FULL_DISPLAY_ARGUMENT = '-f';
13
    const MEDIAINFO_URLENCODE = '--urlencode';
14
    const MEDIAINFO_INCLUDE_COVER_DATA = '--Cover_Data=base64';
15
16
    /**
17
     * @var Process
18
     */
19
    protected $process;
20
21
    /**
22
     * @param Process $process
23
     */
24 8
    public function __construct(Process $process)
25
    {
26 8
        $this->process = $process;
27 8
    }
28
29
    /**
30
     * @throws \RuntimeException
31
     *
32
     * @return string
33
     */
34 2 View Code Duplication
    public function run(): string
35
    {
36 2
        $this->process->run();
37 2
        if (!$this->process->isSuccessful()) {
38 1
            throw new \RuntimeException($this->process->getErrorOutput());
39
        }
40
41 1
        return $this->process->getOutput();
42
    }
43
44
    /**
45
     * Asynchronously start mediainfo operation.
46
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
47
     */
48 2
    public function start(): void
49
    {
50
        // just takes advantage of symfony's underlying Process framework
51
        // process runs in background
52 2
        $this->process->start();
53 2
    }
54
55
    /**
56
     * Blocks until call is complete.
57
     *
58
     * @throws \Exception        If this function is called before start()
59
     * @throws \RuntimeException
60
     *
61
     * @return string
62
     */
63 2 View Code Duplication
    public function wait(): string
64
    {
65
        // blocks here until process completes
66 2
        $this->process->wait();
67
68 2
        if (!$this->process->isSuccessful()) {
69 1
            throw new \RuntimeException($this->process->getErrorOutput());
70
        }
71
72 1
        return $this->process->getOutput();
73
    }
74
}
75