Test Failed
Pull Request — master (#89)
by
unknown
03:32
created

MediaInfoOutputParser::getMediaInfoContainer()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 14.8028

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 10
cts 19
cp 0.5263
rs 8.1475
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 14.8028
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($output)
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($ignoreUnknownTrackTypes = false)
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 (false === array_key_exists('File', $this->parsedOutput)) {
41 1
            throw new MediainfoOutputParsingException(
42
                'XML format of mediainfo >=17.10 command has changed, check php-mediainfo documentation'
43 1
            );
44
        }
45
        
46 3
        if(is_array($this)){
47
          foreach ($this->parsedOutput['File']['track'] as $trackType) {
48
              try {
49
                  if (isset($trackType['@attributes']['type'])) {
50
                      $mediaInfoContainerBuilder->addTrackType($trackType['@attributes']['type'], $trackType);
51
                  }
52
              } catch (UnknownTrackTypeException $ex) {
53
                  if (!$ignoreUnknownTrackTypes) {
54
                      // rethrow exception
55
                      throw $ex;
56
                  }
57
                  // else ignore
58
              }
59
          }
60
        }
61
62 3
        return $mediaInfoContainerBuilder->build();
63
    }
64
}
65