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

AudioStreamCollection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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