Passed
Pull Request — master (#117)
by
unknown
01:34
created

MediaInfo::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Mhor\MediaInfo;
4
5
use Mhor\MediaInfo\Builder\MediaInfoCommandBuilder;
6
use Mhor\MediaInfo\Container\MediaInfoContainer;
7
use Mhor\MediaInfo\Parser\MediaInfoOutputParser;
8
use Mhor\MediaInfo\Runner\MediaInfoCommandRunner;
9
10
class MediaInfo
11
{
12
    /**
13
     * @var MediaInfoCommandRunner|null
14
     */
15
    private $mediaInfoCommandRunnerAsync = null;
16
17
    /**
18
     * @var array
19
     */
20
    private $configuration = [
21
        'command'                            => null,
22
        'use_oldxml_mediainfo_output_format' => true,
23
        'urlencode'                          => false,
24
    ];
25
26
    /**
27
     * Stored original XML output from MediaInfo
28
     *
29
     * @var string
30
     */
31
    private $originalXml = '';
32
33
    /**
34
     * @param $filePath
35
     * @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
36
     *                                      default behavior (false) is throw an exception on unknown track types.
37
     *
38
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
39
     *
40
     * @return MediaInfoContainer
41
     */
42
    public function getInfo($filePath, $ignoreUnknownTrackTypes = false): MediaInfoContainer
43
    {
44
        $mediaInfoCommandBuilder = new MediaInfoCommandBuilder();
45
        $output = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner($filePath, $this->configuration)->run();
46
47
        $this->originalXml = $output;
48
49
        $mediaInfoOutputParser = new MediaInfoOutputParser();
50
        $mediaInfoOutputParser->parse($output);
51
52
        return $mediaInfoOutputParser->getMediaInfoContainer($ignoreUnknownTrackTypes);
53
    }
54
55
    /**
56
     * Call to start asynchronous process.
57
     *
58
     * Make call to MediaInfo::getInfoWaitAsync() afterwards to received MediaInfoContainer object.
59
     *
60
     * @param $filePath
61
     */
62
    public function getInfoStartAsync($filePath): void
63
    {
64
        $mediaInfoCommandBuilder = new MediaInfoCommandBuilder();
65
        $this->mediaInfoCommandRunnerAsync = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner(
66
            $filePath,
67
            $this->configuration
68
        );
69
        $this->mediaInfoCommandRunnerAsync->start();
70
    }
71
72
    /**
73
     * @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
74
     *                                      default behavior (false) is throw an exception on unknown track types.
75
     *
76
     * @throws \Exception                                          If this function is called before getInfoStartAsync()
77
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
78
     *
79
     * @return MediaInfoContainer
80
     */
81
    public function getInfoWaitAsync($ignoreUnknownTrackTypes = false): MediaInfoContainer
82
    {
83
        if ($this->mediaInfoCommandRunnerAsync == null) {
84
            throw new \Exception('You must run `getInfoStartAsync` before running `getInfoWaitAsync`');
85
        }
86
87
        // blocks here until process is complete
88
        $output = $this->mediaInfoCommandRunnerAsync->wait();
89
90
        $mediaInfoOutputParser = new MediaInfoOutputParser();
91
        $mediaInfoOutputParser->parse($output);
92
93
        return $mediaInfoOutputParser->getMediaInfoContainer($ignoreUnknownTrackTypes);
94
    }
95
96
    /**
97
     * @param string $key
98
     * @param string $value
99
     *
100
     * @throws \Exception
101
     */
102 1 View Code Duplication
    public function setConfig($key, $value)
103
    {
104 1
        if (!array_key_exists($key, $this->configuration)) {
105 1
            throw new \Exception(
106 1
                sprintf('key "%s" does\'t exist', $key)
107
            );
108
        }
109
110 1
        $this->configuration[$key] = $value;
111 1
    }
112
113
    /**
114
     * @param $key
115
     *
116
     * @throws \Exception
117
     *
118
     * @return mixed
119
     */
120 2 View Code Duplication
    public function getConfig($key)
121
    {
122 2
        if (!array_key_exists($key, $this->configuration)) {
123 1
            throw new \Exception(
124 1
                sprintf('key "%s" does\'t exist', $key)
125
            );
126
        }
127
128 2
        return $this->configuration[$key];
129
    }
130
131
    /**
132
     * Get last received XML output of MediaInfo
133
     *
134
     * @return string
135
     */
136
    public function getOriginalXml()
137
    {
138
        return $this->originalXml;
139
    }
140
}
141