Passed
Push — master ( 1fcfe0...134786 )
by Sébastien
02:28
created

VideoStreamCollection::count()   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 VideoStreamCollection implements StreamCollectionInterface
11
{
12
    /** @var array<int, array> */
13
    private $streamsMetadata;
14
15
    /** @var array<int, VideoStream> */
16
    private $streams;
17
18
    /**
19
     * @param array<int, array> $videoStreamsMetadata
20
     *
21
     * @throws InvalidStreamMetadataException
22
     */
23 8
    public function __construct(array $videoStreamsMetadata)
24
    {
25 8
        $this->streamsMetadata = $videoStreamsMetadata;
26 8
        $this->loadStreams();
27 7
    }
28
29 6
    public function getFirst(): VideoStream
30
    {
31 6
        if ($this->count() === 0) {
32 1
            throw new NoStreamException('Unable to get video first stream, none exists');
33
        }
34
35 5
        return new VideoStream($this->streamsMetadata[0]);
36
    }
37
38 7
    public function count(): int
39
    {
40 7
        return count($this->streamsMetadata);
41
    }
42
43 2
    public function getIterator(): \ArrayIterator
44
    {
45 2
        return new \ArrayIterator($this->streams);
46
    }
47
48
    /**
49
     * @throws InvalidStreamMetadataException
50
     */
51 8
    private function loadStreams(): void
52
    {
53 8
        $this->streams = [];
54 8
        foreach ($this->streamsMetadata as $idx => $metadata) {
55 7
            if (!is_array($metadata)) {
56 1
                throw new InvalidStreamMetadataException(sprintf(
57 1
                    'Invalid or unsupported metadata stream received %s',
58 1
                    (string) json_encode($metadata)
59
                ));
60
            }
61 6
            $this->streams[] = new VideoStream($metadata);
62
        }
63 7
    }
64
}
65