Completed
Push — make_it_configurable ( bf7a95 )
by Maxime
02:19
created

MediaInfo::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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