MediaInfoOutputParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 53
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 4 1
B getMediaInfoContainer() 0 31 7
1
<?php
2
3
namespace Mhor\MediaInfo\Parser;
4
5
use Mhor\MediaInfo\Builder\MediaInfoContainerBuilder;
6
use Mhor\MediaInfo\Container\MediaInfoContainer;
7
use Mhor\MediaInfo\Exception\MediainfoOutputParsingException;
8
use Mhor\MediaInfo\Exception\UnknownTrackTypeException;
9
10
class MediaInfoOutputParser extends AbstractXmlOutputParser
11
{
12
    /**
13
     * @var array
14
     */
15
    private $parsedOutput;
16
17
    /**
18
     * @param string $output
19
     */
20 4
    public function parse(string $output): void
21
    {
22 4
        $this->parsedOutput = $this->transformXmlToArray($output);
23 4
    }
24
25
    /**
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
     * @return MediaInfoContainer
30
     */
31 5
    public function getMediaInfoContainer(bool $ignoreUnknownTrackTypes = false): \Mhor\MediaInfo\Container\MediaInfoContainer
32
    {
33 5
        if ($this->parsedOutput === null) {
34 1
            throw new \Exception('You must run `parse` before running `getMediaInfoContainer`');
35
        }
36
37 4
        $mediaInfoContainerBuilder = new MediaInfoContainerBuilder();
38 4
        $mediaInfoContainerBuilder->setVersion($this->parsedOutput['@attributes']['version']);
39
40 4
        if (!array_key_exists('File', $this->parsedOutput)) {
41 1
            throw new MediainfoOutputParsingException(
42 1
                'XML format of mediainfo >=17.10 command has changed, check php-mediainfo documentation'
43
            );
44
        }
45
46 3
        foreach ($this->parsedOutput['File']['track'] as $trackType) {
47
            try {
48 3
                if (isset($trackType['@attributes']['type'])) {
49 3
                    $mediaInfoContainerBuilder->addTrackType($trackType['@attributes']['type'], $trackType);
50
                }
51 2
            } catch (UnknownTrackTypeException $ex) {
52 2
                if (!$ignoreUnknownTrackTypes) {
53
                    // rethrow exception
54 3
                    throw $ex;
55
                }
56
                // else ignore
57
            }
58
        }
59
60 2
        return $mediaInfoContainerBuilder->build();
61
    }
62
}
63