Completed
Push — master ( cd82ef...7edba9 )
by Maxime
02:18 queued 43s
created

MediaInfo::getInfoWaitAsync()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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(
55
            $filePath,
56
            $this->configuration
57
        );
58
        $this->mediaInfoCommandRunnerAsync->start();
59
    }
60
61
    /**
62
     * @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
63
     *                                      default behavior (false) is throw an exception on unknown track types.
64
     *
65
     * @throws \Exception                                          If this function is called before getInfoStartAsync()
66
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
67
     *
68
     * @return MediaInfoContainer
69
     */
70
    public function getInfoWaitAsync($ignoreUnknownTrackTypes = false)
71
    {
72
        if ($this->mediaInfoCommandRunnerAsync == null) {
73
            throw new \Exception('You must run `getInfoStartAsync` before running `getInfoWaitAsync`');
74
        }
75
76
        // blocks here until process is complete
77
        $output = $this->mediaInfoCommandRunnerAsync->wait();
78
79
        $mediaInfoOutputParser = new MediaInfoOutputParser();
80
        $mediaInfoOutputParser->parse($output);
81
82
        return $mediaInfoOutputParser->getMediaInfoContainer($ignoreUnknownTrackTypes);
83
    }
84
85
    /**
86
     * @param string $key
87
     * @param string $value
88
     */
89
    public function setConfig($key, $value)
90
    {
91
        if (!array_key_exists($key, $this->configuration)) {
92
            throw new \Exception(
93
                sprintf('key "%s" does\'t exist', $key)
94
            );
95
        }
96
97
        $this->configuration[$key] = $value;
98
    }
99
}
100