Completed
Push — master ( cd82ef...7edba9 )
by Maxime
02:18 queued 43s
created

MediaInfoCommandRunner::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 23
rs 8.7972
cc 4
eloc 16
nc 8
nop 4
1
<?php
2
3
namespace Mhor\MediaInfo\Runner;
4
5
use Symfony\Component\Process\ProcessBuilder;
6
7
class MediaInfoCommandRunner
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $filePath;
13
14
    /**
15
     * @var ProcessBuilder
16
     */
17
    protected $processBuilder;
18
19
    /**
20
     * @var Process
21
     */
22
    protected $processAsync = null;
23
24
    /**
25
     * @var string
26
     */
27
    protected $command = 'mediainfo';
28
29
    /**
30
     * @var array
31
     */
32
    protected $arguments = array('--OUTPUT=XML', '-f');
33
34
    /**
35
     * @param string         $filePath
36
     * @param array          $arguments
37
     * @param ProcessBuilder $processBuilder
38
     */
39
    public function __construct(
40
        $filePath,
41
        $command = null,
42
        array $arguments = null,
43
        $processBuilder = null
44
    ) {
45
        $this->filePath = $filePath;
46
        if ($command !== null) {
47
            $this->command = $command;
48
        }
49
50
        if ($arguments !== null) {
51
            $this->arguments = $arguments;
52
        }
53
54
        if ($processBuilder === null) {
55
            $this->processBuilder = ProcessBuilder::create()
56
            ->setPrefix($this->command)
57
            ->setArguments($this->arguments);
58
        } else {
59
            $this->processBuilder = $processBuilder;
60
        }
61
    }
62
63
    /**
64
     * @throws \RuntimeException
65
     *
66
     * @return string
67
     */
68
    public function run()
69
    {
70
        $lc_ctype = setlocale(LC_CTYPE, 0);
71
        $this->processBuilder->add($this->filePath);
72
        $this->processBuilder->setEnv('LANG', $lc_ctype);
73
        $process = $this->processBuilder->getProcess();
74
        $process->run();
75
        if (!$process->isSuccessful()) {
76
            throw new \RuntimeException($process->getErrorOutput());
77
        }
78
79
        return $process->getOutput();
80
    }
81
82
    /**
83
     * Asynchronously start mediainfo operation.
84
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
85
     */
86
    public function start()
87
    {
88
        $this->processBuilder->add($this->filePath);
89
        $this->processAsync = $this->processBuilder->getProcess();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->processBuilder->getProcess() of type object<Symfony\Component\Process\Process> is incompatible with the declared type object<Mhor\MediaInfo\Runner\Process> of property $processAsync.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        // just takes advantage of symfony's underlying Process framework
91
        // process runs in background
92
        $this->processAsync->start();
93
    }
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
    public function wait()
104
    {
105
        if ($this->processAsync == null) {
106
            throw new \Exception('You must run `start` before running `wait`');
107
        }
108
109
        // blocks here until process completes
110
        $this->processAsync->wait();
111
112
        if (!$this->processAsync->isSuccessful()) {
113
            throw new \RuntimeException($this->processAsync->getErrorOutput());
114
        }
115
116
        return $this->processAsync->getOutput();
117
    }
118
}
119