Failed Conditions
Push — master ( 34f957...606500 )
by Sébastien
02:10
created

SubtitleStreamCollection::loadStreams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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 SubtitleStreamCollection implements SubtitleStreamCollectionInterface
11
{
12
    /** @var array<int, array> */
13
    private $streamsMetadata;
14
15
    /** @var array<int, SubtitleStreamInterface> */
16
    private $streams;
17
18
    /**
19
     * @param array<int, array> $subtitleStreamsMetadata
20
     *
21
     * @throws InvalidStreamMetadataException
22
     */
23 4
    public function __construct(array $subtitleStreamsMetadata)
24
    {
25 4
        $this->streamsMetadata = $subtitleStreamsMetadata;
26 4
        $this->loadStreams();
27 3
    }
28
29 2
    public function getFirst(): SubtitleStreamInterface
30
    {
31 2
        if ($this->count() === 0) {
32 1
            throw new NoStreamException('Unable to get video subtitle stream, none exists');
33
        }
34
35 1
        return new SubtitleStream($this->streamsMetadata[0]);
36
    }
37
38 3
    public function count(): int
39
    {
40 3
        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 4
    private function loadStreams(): void
52
    {
53 4
        $this->streams = [];
54 4
        foreach ($this->streamsMetadata as $idx => $metadata) {
55 3
            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 2
            $this->streams[] = new SubtitleStream($metadata);
62
        }
63 3
    }
64
}
65