Passed
Branch master (ebf0bf)
by Maxime
01:35
created

MediaInfoCommandRunner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 119
ccs 39
cts 42
cp 0.9286
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 37 5
A run() 0 13 2
A start() 0 6 1
A wait() 0 15 3
1
<?php
2
3
namespace Mhor\MediaInfo\Runner;
4
5
use Symfony\Component\Process\Process;
6
7
class MediaInfoCommandRunner
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $filePath;
13
14
    /**
15
     * @var Process
16
     */
17
    protected $process;
18
19
    /**
20
     * @var string
21
     */
22
    protected $command = 'mediainfo';
23
24
    /**
25
     * @var array
26
     */
27
    protected $arguments = ['--OUTPUT=XML', '-f'];
28
29
    /**
30
     * @param string  $filePath
31
     * @param array   $arguments
32
     * @param Process $process
33
     */
34 6
    public function __construct(
35
        $filePath,
36
        $command = null,
37
        array $arguments = null,
38
        Process $process = null
39
    ) {
40 6
        $this->filePath = $filePath;
41 6
        if ($command !== null) {
42 2
            $this->command = $command;
43 2
        }
44
45 6
        if ($arguments !== null) {
46 3
            $this->arguments = $arguments;
47 3
        }
48
49 6
        array_unshift($this->arguments, $this->filePath);
50
51 6
        if (method_exists('Symfony\\Component\\Process\\ProcessUtils', 'escapeArgument')) {
52
            // Symfony 2 compatibility
53 6
            $input = implode(' ', array_map(['Symfony\\Component\\Process\\ProcessUtils', 'escapeArgument'], $this->arguments));
54 6
        } else {
55
            $input = new \ArrayIterator($this->arguments);
56
        }
57
58 6
        if (null !== $process) {
59 3
            $process->setCommandLine($this->command);
60 3
            $process->setInput($input);
61 3
            $this->process = $process;
62 3
        } else {
63 3
            $this->process = new Process(
64 3
                $this->command,
65 3
                null,
66 3
                null,
67
                $input
68 3
            );
69
        }
70 6
    }
71
72
    /**
73
     * @throws \RuntimeException
74
     *
75
     * @return string
76
     */
77 2
    public function run()
78
    {
79
        $env = [
80 2
            'LANG' => setlocale(LC_CTYPE, 0),
81 2
        ];
82 2
        $this->process->setEnv($env);
83 2
        $this->process->run();
84 2
        if (!$this->process->isSuccessful()) {
85 1
            throw new \RuntimeException($this->process->getErrorOutput());
86
        }
87
88 1
        return $this->process->getOutput();
89
    }
90
91
    /**
92
     * Asynchronously start mediainfo operation.
93
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
94
     */
95 1
    public function start()
96
    {
97
        // just takes advantage of symfony's underlying Process framework
98
        // process runs in background
99 1
        $this->process->start();
100 1
    }
101
102
    /**
103
     * Blocks until call is complete.
104
     *
105
     * @throws \Exception        If this function is called before start()
106
     * @throws \RuntimeException
107
     *
108
     * @return string
109
     */
110 1
    public function wait()
111
    {
112 1
        if ($this->process == null) {
113
            throw new \Exception('You must run `start` before running `wait`');
114
        }
115
116
        // blocks here until process completes
117 1
        $this->process->wait();
118
119 1
        if (!$this->process->isSuccessful()) {
120
            throw new \RuntimeException($this->process->getErrorOutput());
121
        }
122
123 1
        return $this->process->getOutput();
124
    }
125
}
126