Passed
Push — fix_process_component ( aa65d1 )
by Maxime
01:58
created

MediaInfoCommandRunner::start()   A

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
14
    /**
15
     * @var Process
16
     */
17
    protected $process;
18
19
    /**
20
     * @param Process $process
21
     */
22 8
    public function __construct(Process $process)
23
    {
24 8
        $this->process = $process;
25 8
    }
26
27
    /**
28
     * @throws \RuntimeException
29
     *
30
     * @return string
31
     */
32 2 View Code Duplication
    public function run()
33
    {
34 2
        $this->process->run();
35 2
        if (!$this->process->isSuccessful()) {
36 1
            throw new \RuntimeException($this->process->getErrorOutput());
37
        }
38
39 1
        return $this->process->getOutput();
40
    }
41
42
    /**
43
     * Asynchronously start mediainfo operation.
44
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
45
     */
46 2
    public function start()
47
    {
48
        // just takes advantage of symfony's underlying Process framework
49
        // process runs in background
50 2
        $this->process->start();
51 2
    }
52
53
    /**
54
     * Blocks until call is complete.
55
     *
56
     * @throws \Exception        If this function is called before start()
57
     * @throws \RuntimeException
58
     *
59
     * @return string
60
     */
61 2 View Code Duplication
    public function wait()
62
    {
63
        // blocks here until process completes
64 2
        $this->process->wait();
65
66 2
        if (!$this->process->isSuccessful()) {
67 1
            throw new \RuntimeException($this->process->getErrorOutput());
68
        }
69
70 1
        return $this->process->getOutput();
71
    }
72
}
73