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