Passed
Push — master ( 3b2ed5...ac7e73 )
by Sébastien
02:17
created

VideoStreamCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 51
c 0
b 0
f 0
ccs 16
cts 20
cp 0.8
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\StreamNotFoundException;
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
     * @throws InvalidStreamMetadataException
21
     */
22 2
    public function __construct(array $videoStreamsMetadata)
23
    {
24 2
        $this->streamsMetadata = $videoStreamsMetadata;
25 2
        $this->loadStreams();
26 2
    }
27
28 1
    public function getFirst(): VideoStream
29
    {
30 1
        if ($this->count() === 0) {
31
            throw new StreamNotFoundException('Unable to get video first stream, none exists');
32
        }
33
34 1
        return new VideoStream($this->streamsMetadata[0]);
35
    }
36
37 2
    public function count(): int
38
    {
39 2
        return count($this->streamsMetadata);
40
    }
41
42 1
    public function getIterator(): \ArrayIterator
43
    {
44 1
        return new \ArrayIterator($this->streams);
45
    }
46
47
    /**
48
     * @throws InvalidStreamMetadataException
49
     */
50 2
    private function loadStreams(): void
51
    {
52 2
        $this->streams = [];
53 2
        foreach ($this->streamsMetadata as $idx => $metadata) {
54 2
            if (!is_array($metadata)) {
55
                throw new InvalidStreamMetadataException(sprintf(
56
                    'Invalid or unsupported metadata stream received %s',
57
                    (string) json_encode($metadata)
58
                ));
59
            }
60 2
            $this->streams[] = new VideoStream($metadata);
61
        }
62 2
    }
63
}
64