AudioStreamCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
final 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 5
    public function __construct(array $audioStreamsMetadata)
24
    {
25 5
        $this->streamsMetadata = $audioStreamsMetadata;
26 5
        $this->loadStreams();
27 4
    }
28
29
    /**
30
     * @throws NoStreamException
31
     */
32 3
    public function getFirst(): AudioStreamInterface
33
    {
34 3
        if ($this->count() === 0) {
35 1
            throw new NoStreamException('Unable to get video first stream, none exists');
36
        }
37
38 2
        return new AudioStream($this->streamsMetadata[0]);
39
    }
40
41 4
    public function count(): int
42
    {
43 4
        return count($this->streamsMetadata);
44
    }
45
46
    /**
47
     * @return \ArrayIterator<int, AudioStreamInterface>
48
     */
49 2
    public function getIterator(): \ArrayIterator
50
    {
51 2
        return new \ArrayIterator($this->streams);
52
    }
53
54
    /**
55
     * @throws InvalidStreamMetadataException
56
     */
57 5
    private function loadStreams(): void
58
    {
59 5
        $this->streams = [];
60 5
        foreach ($this->streamsMetadata as $idx => $metadata) {
61 4
            if (!is_array($metadata)) {
62 1
                throw new InvalidStreamMetadataException(sprintf(
63 1
                    'Invalid or unsupported metadata stream received %s',
64 1
                    (string) json_encode($metadata)
65
                ));
66
            }
67 3
            $this->streams[] = new AudioStream($metadata);
68
        }
69 4
    }
70
}
71