Passed
Push — update-phpunit ( 55afda )
by Maxime
02:53
created

MediaInfoCommandRunner::__construct()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.0023

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 24
cts 25
cp 0.96
rs 8.4905
c 0
b 0
f 0
cc 6
nc 32
nop 5
crap 6.0023
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 6
    public function __construct(
40
        $filePath,
41
        $command = null,
42
        array $arguments = null,
43
        Process $process = null,
44
        $forceOldXmlOutput = false
45
    ) {
46 6
        $this->filePath = $filePath;
47 6
        if ($command !== null) {
48 2
            $this->command = $command;
49
        }
50
51 6
        $this->arguments = self::XML_OUTPUT_FORMAT_ARGUMENTS;
52 6
        if ($forceOldXmlOutput) {
53
            $this->arguments = self::FORCED_OLDXML_OUTPUT_FORMAT_ARGUMENTS;
54
        }
55
56 6
        if ($arguments !== null) {
57 3
            $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 6
        $args = $this->arguments;
64 6
        array_unshift($args, $this->filePath);
65
66
        $env = [
67 6
            'LANG' => setlocale(LC_CTYPE, 0),
68
        ];
69 6
        $finalCommand = [$this->command];
70
71 6
        $i = 0;
72 6
        foreach ($args as $value) {
73 6
            $var = 'MEDIAINFO_VAR_'.$i++;
74 6
            $finalCommand[] = '"$'.$var.'"';
75 6
            $env[$var] = $value;
76
        }
77
78 6
        $finalCommandString = implode(' ', $finalCommand);
79
80 6
        if (null !== $process) {
81 3
            $process->setCommandLine($finalCommandString);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...ocess::setCommandLine() has been deprecated with message: since Symfony 4.2.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82 3
            $process->setEnv($env);
83 3
            $this->process = $process;
84
        } else {
85 3
            $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 6
    }
88
89
    /**
90
     * @throws \RuntimeException
91
     *
92
     * @return string
93
     */
94 2
    public function run()
95
    {
96 2
        $this->process->run();
97 2
        if (!$this->process->isSuccessful()) {
98 1
            throw new \RuntimeException($this->process->getErrorOutput());
99
        }
100
101 1
        return $this->process->getOutput();
102
    }
103
104
    /**
105
     * Asynchronously start mediainfo operation.
106
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
107
     */
108 1
    public function start()
109
    {
110
        // just takes advantage of symfony's underlying Process framework
111
        // process runs in background
112 1
        $this->process->start();
113 1
    }
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 1
    public function wait()
124
    {
125 1
        if ($this->process == null) {
126
            throw new \Exception('You must run `start` before running `wait`');
127
        }
128
129
        // blocks here until process completes
130 1
        $this->process->wait();
131
132 1
        if (!$this->process->isSuccessful()) {
133
            throw new \RuntimeException($this->process->getErrorOutput());
134
        }
135
136 1
        return $this->process->getOutput();
137
    }
138
}
139