Passed
Branch master (c5fb66)
by Maxime
01:46
created

MediaInfo::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
    ];
24
25
    /**
26
     * @param $filePath
27
     * @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
28
     *                                      default behavior (false) is throw an exception on unknown track types.
29
     *
30
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
31
     *
32
     * @return MediaInfoContainer
33
     */
34
    public function getInfo($filePath, $ignoreUnknownTrackTypes = false): MediaInfoContainer
35
    {
36
        $mediaInfoCommandBuilder = new MediaInfoCommandBuilder();
37
        $output = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner($filePath, $this->configuration)->run();
38
39
        $mediaInfoOutputParser = new MediaInfoOutputParser();
40
        $mediaInfoOutputParser->parse($output);
41
42
        return $mediaInfoOutputParser->getMediaInfoContainer($ignoreUnknownTrackTypes);
43
    }
44
45
    /**
46
     * Call to start asynchronous process.
47
     *
48
     * Make call to MediaInfo::getInfoWaitAsync() afterwards to received MediaInfoContainer object.
49
     *
50
     * @param $filePath
51
     */
52
    public function getInfoStartAsync($filePath): void
53
    {
54
        $mediaInfoCommandBuilder = new MediaInfoCommandBuilder();
55
        $this->mediaInfoCommandRunnerAsync = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner(
56
            $filePath,
57
            $this->configuration
58
        );
59
        $this->mediaInfoCommandRunnerAsync->start();
60
    }
61
62
    /**
63
     * @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
64
     *                                      default behavior (false) is throw an exception on unknown track types.
65
     *
66
     * @throws \Exception                                          If this function is called before getInfoStartAsync()
67
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
68
     *
69
     * @return MediaInfoContainer
70
     */
71
    public function getInfoWaitAsync($ignoreUnknownTrackTypes = false): MediaInfoContainer
72
    {
73
        if ($this->mediaInfoCommandRunnerAsync == null) {
74
            throw new \Exception('You must run `getInfoStartAsync` before running `getInfoWaitAsync`');
75
        }
76
77
        // blocks here until process is complete
78
        $output = $this->mediaInfoCommandRunnerAsync->wait();
79
80
        $mediaInfoOutputParser = new MediaInfoOutputParser();
81
        $mediaInfoOutputParser->parse($output);
82
83
        return $mediaInfoOutputParser->getMediaInfoContainer($ignoreUnknownTrackTypes);
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param string $value
89
     *
90
     * @throws \Exception
91
     */
92 1
    public function setConfig($key, $value)
93
    {
94 1
        if (!array_key_exists($key, $this->configuration)) {
95 1
            throw new \Exception(
96 1
                sprintf('key "%s" does\'t exist', $key)
97
            );
98
        }
99
100 1
        $this->configuration[$key] = $value;
101 1
    }
102
}
103