Passed
Push — fix_process_component ( aa65d1 )
by Maxime
01:58
created

MediaInfoCommandRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 30.3 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 20
loc 66
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 9 9 2
A start() 0 6 1
A wait() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mhor\MediaInfo\Runner;
4
5
use Symfony\Component\Process\Process;
6
7
class MediaInfoCommandRunner
8
{
9
    const MEDIAINFO_COMMAND = 'mediainfo';
10
    const MEDIAINFO_OLDXML_OUTPUT_ARGUMENT = '--OUTPUT=OLDXML';
11
    const MEDIAINFO_XML_OUTPUT_ARGUMENT = '--OUTPUT=XML';
12
    const MEDIAINFO_FULL_DISPLAY_ARGUMENT = '-f';
13
14
    /**
15
     * @var Process
16
     */
17
    protected $process;
18
19
    /**
20
     * @param Process $process
21
     */
22 8
    public function __construct(Process $process)
23
    {
24 8
        $this->process = $process;
25 8
    }
26
27
    /**
28
     * @throws \RuntimeException
29
     *
30
     * @return string
31
     */
32 2 View Code Duplication
    public function run()
33
    {
34 2
        $this->process->run();
35 2
        if (!$this->process->isSuccessful()) {
36 1
            throw new \RuntimeException($this->process->getErrorOutput());
37
        }
38
39 1
        return $this->process->getOutput();
40
    }
41
42
    /**
43
     * Asynchronously start mediainfo operation.
44
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
45
     */
46 2
    public function start()
47
    {
48
        // just takes advantage of symfony's underlying Process framework
49
        // process runs in background
50 2
        $this->process->start();
51 2
    }
52
53
    /**
54
     * Blocks until call is complete.
55
     *
56
     * @throws \Exception        If this function is called before start()
57
     * @throws \RuntimeException
58
     *
59
     * @return string
60
     */
61 2 View Code Duplication
    public function wait()
62
    {
63
        // blocks here until process completes
64 2
        $this->process->wait();
65
66 2
        if (!$this->process->isSuccessful()) {
67 1
            throw new \RuntimeException($this->process->getErrorOutput());
68
        }
69
70 1
        return $this->process->getOutput();
71
    }
72
}
73