Completed
Pull Request — master (#83)
by
unknown
06:39
created

MediaInfoCommandRunner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 112
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A wait() 0 15 3
B __construct() 0 34 5
A run() 0 9 2
A start() 0 6 1
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
        $args = $this->arguments;
50 6
        array_unshift($args, $this->filePath);
51
52
        $env = [
53 6
            'LANG' => setlocale(LC_CTYPE, 0),
54 6
        ];
55 6
        $finalCommand = [$this->command];
56
57 6
        foreach ($args as $value) {
58 6
            $finalCommand[] = $value;
59 6
        }
60
61 6
        if (null !== $process) {
62 3
            $process->setEnv($env);
63 3
            $this->process = $process;
64 3
        } else {
65 3
            $this->process = new Process($finalCommand, null, $env);
66
        }
67 6
    }
68
69
    /**
70
     * @throws \RuntimeException
71
     *
72
     * @return string
73
     */
74 2
    public function run()
75
    {
76 2
        $this->process->run();
77 2
        if (!$this->process->isSuccessful()) {
78 1
            throw new \RuntimeException($this->process->getErrorOutput());
79
        }
80
81 1
        return $this->process->getOutput();
82
    }
83
84
    /**
85
     * Asynchronously start mediainfo operation.
86
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
87
     */
88 1
    public function start()
89
    {
90
        // just takes advantage of symfony's underlying Process framework
91
        // process runs in background
92 1
        $this->process->start();
93 1
    }
94
95
    /**
96
     * Blocks until call is complete.
97
     *
98
     * @throws \Exception        If this function is called before start()
99
     * @throws \RuntimeException
100
     *
101
     * @return string
102
     */
103 1
    public function wait()
104
    {
105 1
        if ($this->process == null) {
106
            throw new \Exception('You must run `start` before running `wait`');
107
        }
108
109
        // blocks here until process completes
110 1
        $this->process->wait();
111
112 1
        if (!$this->process->isSuccessful()) {
113
            throw new \RuntimeException($this->process->getErrorOutput());
114
        }
115
116 1
        return $this->process->getOutput();
117
    }
118
}
119