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

MediaInfoOutputParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 55
ccs 13
cts 22
cp 0.5909
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 4 1
B getMediaInfoContainer() 0 33 8
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