MediaInfoCommandRunner   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 20
loc 68
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
    const MEDIAINFO_URLENCODE = '--urlencode';
14
    const MEDIAINFO_INCLUDE_COVER_DATA = '--Cover_Data=base64';
15
16
    /**
17
     * @var Process
18
     */
19
    protected $process;
20
21
    /**
22
     * @param Process $process
23
     */
24 8
    public function __construct(Process $process)
25
    {
26 8
        $this->process = $process;
27 8
    }
28
29
    /**
30
     * @throws \RuntimeException
31
     *
32
     * @return string
33
     */
34 2 View Code Duplication
    public function run(): string
35
    {
36 2
        $this->process->run();
37 2
        if (!$this->process->isSuccessful()) {
38 1
            throw new \RuntimeException($this->process->getErrorOutput());
39
        }
40
41 1
        return $this->process->getOutput();
42
    }
43
44
    /**
45
     * Asynchronously start mediainfo operation.
46
     * Make call to MediaInfoCommandRunner::wait() afterwards to receive output.
47
     */
48 2
    public function start(): void
49
    {
50
        // just takes advantage of symfony's underlying Process framework
51
        // process runs in background
52 2
        $this->process->start();
53 2
    }
54
55
    /**
56
     * Blocks until call is complete.
57
     *
58
     * @throws \Exception        If this function is called before start()
59
     * @throws \RuntimeException
60
     *
61
     * @return string
62
     */
63 2 View Code Duplication
    public function wait(): string
64
    {
65
        // blocks here until process completes
66 2
        $this->process->wait();
67
68 2
        if (!$this->process->isSuccessful()) {
69 1
            throw new \RuntimeException($this->process->getErrorOutput());
70
        }
71
72 1
        return $this->process->getOutput();
73
    }
74
}
75