Test Failed
Pull Request — 6.0.x (#108)
by Maxime
03:28 queued 01:42
created

MediaInfoCommandBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 74
ccs 9
cts 27
cp 0.3333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMediaInfoCommandRunner() 0 28 4
A buildMediaInfoProcess() 0 27 3
1
<?php
2
3
namespace Mhor\MediaInfo\Builder;
4
5
use Mhor\MediaInfo\Runner\MediaInfoCommandRunner;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Process\Process;
8
9
class MediaInfoCommandBuilder
10
{
11
    /**
12
     * @param string $filePath
13
     * @param array  $configuration
14
     *
15
     * @throws \Exception
16
     *
17
     * @return MediaInfoCommandRunner
18
     */
19 2
    public function buildMediaInfoCommandRunner(string $filePath, array $configuration = []): MediaInfoCommandRunner
20
    {
21 2
        if (filter_var($filePath, FILTER_VALIDATE_URL) === false) {
22 2
            $fileSystem = new Filesystem();
23
24 2
            if (!$fileSystem->exists($filePath)) {
25 1
                throw new \Exception(sprintf('File "%s" does not exist', $filePath));
26
            }
27
28 1
            if (is_dir($filePath)) {
29 1
                throw new \Exception(sprintf(
30 1
                    'Expected a filename, got "%s", which is a directory',
31 1
                    $filePath
32
                ));
33
            }
34
        }
35
36
        $configuration = $configuration + [
37
            'command'                            => null,
38
            'use_oldxml_mediainfo_output_format' => true,
39
        ];
40
41
        return new MediaInfoCommandRunner($this->buildMediaInfoProcess(
42
            $filePath,
43
            $configuration['command'],
44
            $configuration['use_oldxml_mediainfo_output_format']
45
        ));
46
    }
47
48
    /**
49
     * @param string      $filePath
50
     * @param string|null $command
51
     * @param bool        $forceOldXmlOutput
52
     *
53
     * @return Process
54
     */
55
    private function buildMediaInfoProcess(string $filePath, string $command = null, array $arguments = null, bool $forceOldXmlOutput = true): Process
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57
        if ($command === null) {
58
            $command = MediaInfoCommandRunner::MEDIAINFO_COMMAND;
59
        }
60
61
        // arguments are given through ENV vars in order to have system escape them
62
        $arguments = [
63
            'MEDIAINFO_VAR_FILE_PATH'    => $filePath,
64
            'MEDIAINFO_VAR_FULL_DISPLAY' => MediaInfoCommandRunner::MEDIAINFO_FULL_DISPLAY_ARGUMENT,
65
            'MEDIAINFO_VAR_OUTPUT'       => MediaInfoCommandRunner::MEDIAINFO_OLDXML_OUTPUT_ARGUMENT,
66
        ];
67
68
        if ($forceOldXmlOutput === false) {
69
            $arguments['MEDIAINFO_VAR_OUTPUT'] = MediaInfoCommandRunner::MEDIAINFO_XML_OUTPUT_ARGUMENT;
70
        }
71
72
        $env = $arguments + [
73
            'LANG' => setlocale(LC_CTYPE, 0),
74
        ];
75
76
        return new Process(
77
            array_merge([$command], array_values($arguments)),
78
            null,
79
            $env
80
        );
81
    }
82
}
83