MediaInfo::getInfoWaitAsync()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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