MediaInfoCommandBuilder::buildMediaInfoProcess()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 17
cts 17
cp 1
rs 9.0488
c 0
b 0
f 0
cc 5
nc 16
nop 5
crap 5
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 6
    public function buildMediaInfoCommandRunner(string $filePath, array $configuration = []): MediaInfoCommandRunner
20
    {
21 6
        if (filter_var($filePath, FILTER_VALIDATE_URL) === false) {
22 4
            $fileSystem = new Filesystem();
23
24 4
            if (!$fileSystem->exists($filePath)) {
25 1
                throw new \Exception(sprintf('File "%s" does not exist', $filePath));
26
            }
27
28 3
            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 += [
37 4
            'command'                            => null,
38
            'use_oldxml_mediainfo_output_format' => true,
39
            'urlencode'                          => false,
40
            'include_cover_data'                 => false,
41
        ];
42
43 4
        return new MediaInfoCommandRunner($this->buildMediaInfoProcess(
44 4
            $filePath,
45 4
            $configuration['command'],
46 4
            $configuration['use_oldxml_mediainfo_output_format'],
47 4
            $configuration['urlencode'],
48 4
            $configuration['include_cover_data']
49
        ));
50
    }
51
52
    /**
53
     * @param string      $filePath
54
     * @param string|null $command
55
     * @param bool        $forceOldXmlOutput
56
     * @param bool        $urlencode
57
     *
58
     * @return Process
59
     */
60 4
    private function buildMediaInfoProcess(string $filePath, string $command = null, bool $forceOldXmlOutput = true, bool $urlencode = false, bool $includeCoverData = false): Process
61
    {
62 4
        if ($command === null) {
63 3
            $command = MediaInfoCommandRunner::MEDIAINFO_COMMAND;
64
        }
65
66
        // arguments are given through ENV vars in order to have system escape them
67
        $arguments = [
68 4
            'MEDIAINFO_VAR_FILE_PATH'    => $filePath,
69 4
            'MEDIAINFO_VAR_FULL_DISPLAY' => MediaInfoCommandRunner::MEDIAINFO_FULL_DISPLAY_ARGUMENT,
70 4
            'MEDIAINFO_VAR_OUTPUT'       => MediaInfoCommandRunner::MEDIAINFO_OLDXML_OUTPUT_ARGUMENT,
71
        ];
72
73 4
        if (!$forceOldXmlOutput) {
74 1
            $arguments['MEDIAINFO_VAR_OUTPUT'] = MediaInfoCommandRunner::MEDIAINFO_XML_OUTPUT_ARGUMENT;
75
        }
76
77 4
        if ($urlencode) {
78 1
            $arguments['MEDIAINFO_VAR_URLENCODE'] = MediaInfoCommandRunner::MEDIAINFO_URLENCODE;
79
        }
80
81 4
        if ($includeCoverData) {
82 1
            $arguments['MEDIAINFO_COVER_DATA'] = MediaInfoCommandRunner::MEDIAINFO_INCLUDE_COVER_DATA;
83
        }
84
85
        $env = $arguments + [
86 4
            'LANG' => setlocale(LC_CTYPE, '0'),
87
        ];
88
89 4
        return new Process(
90 4
            array_merge([$command], array_values($arguments)),
91 4
            null,
92 4
            $env
93
        );
94
    }
95
}
96