Passed
Push — master ( 37db96...3b2ed5 )
by Sébastien
02:26 queued 17s
created

VideoStreamCollection::__construct()   A

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\StreamNotFoundException;
8
9
class VideoStreamCollection implements StreamCollectionInterface
10
{
11
    /** @var array<int, array> */
12
    private $streamsMetadata;
13
14
    /** @var array<int, VideoStream> */
15
    private $streams;
16
17
    /**
18
     * @param array<int, array> $videoStreamsMetadata
19
     */
20 1
    public function __construct(array $videoStreamsMetadata)
21
    {
22 1
        $this->streamsMetadata = $videoStreamsMetadata;
23 1
        $this->loadStreams();
24 1
    }
25
26 1
    public function getFirst(): VideoStream
27
    {
28 1
        if ($this->count() === 0) {
29
            throw new StreamNotFoundException('Unable to get video first stream, none exists');
30
        }
31
32 1
        return new VideoStream($this->streamsMetadata[0]);
33
    }
34
35 1
    public function count(): int
36
    {
37 1
        return count($this->streamsMetadata);
38
    }
39
40
    public function getIterator(): \ArrayIterator
41
    {
42
        return new \ArrayIterator($this->streams);
43
    }
44
45 1
    private function loadStreams(): void
46
    {
47 1
        $this->streams = [];
48 1
        foreach ($this->streamsMetadata as $idx => $metadata) {
49 1
            $this->streams[] = new VideoStream($metadata);
50
        }
51 1
    }
52
}
53