SubtitleStreamCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirst() 0 7 2
A __construct() 0 4 1
A count() 0 3 1
A getIterator() 0 3 1
A loadStreams() 0 11 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
final 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 5
    public function __construct(array $subtitleStreamsMetadata)
24
    {
25 5
        $this->streamsMetadata = $subtitleStreamsMetadata;
26 5
        $this->loadStreams();
27 4
    }
28
29
    /**
30
     * @throws NoStreamException
31
     */
32 3
    public function getFirst(): SubtitleStreamInterface
33
    {
34 3
        if ($this->count() === 0) {
35 1
            throw new NoStreamException('Unable to get video subtitle stream, none exists');
36
        }
37
38 2
        return new SubtitleStream($this->streamsMetadata[0]);
39
    }
40
41 4
    public function count(): int
42
    {
43 4
        return count($this->streamsMetadata);
44
    }
45
46
    /**
47
     * @return \ArrayIterator<int, SubtitleStreamInterface>
48
     */
49 2
    public function getIterator(): \ArrayIterator
50
    {
51 2
        return new \ArrayIterator($this->streams);
52
    }
53
54
    /**
55
     * @throws InvalidStreamMetadataException
56
     */
57 5
    private function loadStreams(): void
58
    {
59 5
        $this->streams = [];
60 5
        foreach ($this->streamsMetadata as $idx => $metadata) {
61 4
            if (!is_array($metadata)) {
62 1
                throw new InvalidStreamMetadataException(sprintf(
63 1
                    'Invalid or unsupported metadata stream received %s',
64 1
                    (string) json_encode($metadata)
65
                ));
66
            }
67 3
            $this->streams[] = new SubtitleStream($metadata);
68
        }
69 4
    }
70
}
71