Passed
Push — master ( 4eae52...59c295 )
by Sébastien
03:55
created

AudioStreamCollection::loadStreams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 5
cts 8
cp 0.625
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.4746
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Info;
6
7
use Soluble\MediaTools\Video\Exception\InvalidStreamMetadataException;
8
use Soluble\MediaTools\Video\Exception\NoStreamException;
9
10
class AudioStreamCollection implements AudioStreamCollectionInterface
11
{
12
    /** @var array<int, array> */
13
    private $streamsMetadata;
14
15
    /** @var array<int, AudioStreamInterface> */
16
    private $streams;
17
18
    /**
19
     * @param array<int, array> $audioStreamsMetadata
20
     *
21
     * @throws InvalidStreamMetadataException
22
     */
23 1
    public function __construct(array $audioStreamsMetadata)
24
    {
25 1
        $this->streamsMetadata = $audioStreamsMetadata;
26 1
        $this->loadStreams();
27 1
    }
28
29
    public function getFirst(): AudioStreamInterface
30
    {
31
        if ($this->count() === 0) {
32
            throw new NoStreamException('Unable to get video first stream, none exists');
33
        }
34
35
        return new AudioStream($this->streamsMetadata[0]);
36
    }
37
38 1
    public function count(): int
39
    {
40 1
        return count($this->streamsMetadata);
41
    }
42
43 1
    public function getIterator(): \ArrayIterator
44
    {
45 1
        return new \ArrayIterator($this->streams);
46
    }
47
48
    /**
49
     * @throws InvalidStreamMetadataException
50
     */
51 1
    private function loadStreams(): void
52
    {
53 1
        $this->streams = [];
54 1
        foreach ($this->streamsMetadata as $idx => $metadata) {
55 1
            if (!is_array($metadata)) {
56
                throw new InvalidStreamMetadataException(sprintf(
57
                    'Invalid or unsupported metadata stream received %s',
58
                    (string) json_encode($metadata)
59
                ));
60
            }
61 1
            $this->streams[] = new AudioStream($metadata);
62
        }
63 1
    }
64
}
65