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

VideoStreamCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 52
c 0
b 0
f 0
ccs 20
cts 20
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A getIterator() 0 3 1
A __construct() 0 4 1
A loadStreams() 0 11 3
A getFirst() 0 7 2
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