Test Failed
Pull Request — 6.0.x (#102)
by Maxime
02:15 queued 48s
created

MediaInfoCommandRunner::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Mhor\MediaInfo\Runner;
4
5
use Symfony\Component\Process\Process;
6
7
class MediaInfoCommandRunner
8
{
9
    const FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS = ['--OUTPUT=OLDXML', '-f'];
10
    const XML_OUTPUT_FORMAT_ARGUMENTS = ['--OUTPUT=XML', '-f'];
11
12
    /**
13
     * @var string
14
     */
15
    protected $filePath;
16
17
    /**
18
     * @var Process
19
     */
20
    protected $process;
21
22
    /**
23
     * @var string
24
     */
25
    protected $command = 'mediainfo';
26
27
    /**
28
     * @var array
29
     */
30
    protected $arguments = [];
31
32
    /**
33
     * @param string  $filePath
34
     * @param string  $command
35
     * @param array   $arguments
36
     * @param Process $process
37
     * @param bool    $forceOldXmlOutput
38
     */
39
    public function __construct(
40
        $filePath,
41
        $command = null,
42
        array $arguments = null,
43
        Process $process = null,
44
        $forceOldXmlOutput = false
45
    ) {
46
        $this->filePath = $filePath;
47
        if ($command !== null) {
48
            $this->command = $command;
49
        }
50
51
        $this->arguments = self::XML_OUTPUT_FORMAT_ARGUMENTS;
52
        if ($forceOldXmlOutput) {
53
            $this->arguments = self::FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS;
54
        }
55
56
        if ($arguments !== null) {
57
            $this->arguments = $arguments;
58
        }
59
60
        // /path/to/mediainfo $MEDIAINFO_VAR0 $MEDIAINFO_VAR1...
61
        // args are given through ENV vars in order to have system escape them
62
63
        $args = $this->arguments;
64
        array_unshift($args, $this->filePath);
65
66
        $env = [
67
            'LANG' => setlocale(LC_CTYPE, 0),
68
        ];
69
        $finalCommand = [$this->command];
70
71
        $i = 0;
72
        foreach ($args as $value) {
73
            $var = 'MEDIAINFO_VAR_'.$i++;
74
            $finalCommand[] = '"$'.$var.'"';
75
            $env[$var] = $value;
76
        }
77
78
        $finalCommandString = implode(' ', $finalCommand);
79
80
        if (null !== $process) {
81
            $process->setCommandLine($finalCommandString);
0 ignored issues
show
Bug introduced by
The method setCommandLine() does not seem to exist on object<Symfony\Component\Process\Process>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            $process->setEnv($env);
83
            $this->process = $process;
84
        } else {
85
            $this->process = new Process($finalCommandString, null, $env);
0 ignored issues
show
Documentation introduced by
$finalCommandString is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
        }
87
    }
88
89
    /**
90
     * @throws \RuntimeException
91
     *
92
     * @return string
93
     */
94
    public function run()
95
    {
96
        $this->process->run();
97
        if (!$this->process->isSuccessful()) {
98
            throw new \RuntimeException($this->process->getErrorOutput());
99
        }
100
101
        return $this->process->getOutput();
102
    }
103
104
    /**
105
     * Asynchronously start mediainfo operation.
106
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
107
     */
108
    public function start()
109
    {
110
        // just takes advantage of symfony's underlying Process framework
111
        // process runs in background
112
        $this->process->start();
113
    }
114
115
    /**
116
     * Blocks until call is complete.
117
     *
118
     * @throws \Exception        If this function is called before start()
119
     * @throws \RuntimeException
120
     *
121
     * @return string
122
     */
123
    public function wait()
124
    {
125
        if ($this->process == null) {
126
            throw new \Exception('You must run `start` before running `wait`');
127
        }
128
129
        // blocks here until process completes
130
        $this->process->wait();
131
132
        if (!$this->process->isSuccessful()) {
133
            throw new \RuntimeException($this->process->getErrorOutput());
134
        }
135
136
        return $this->process->getOutput();
137
    }
138
}
139