VideoStreamCollection   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 9
eloc 23
c 4
b 0
f 3
dl 0
loc 69
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A __construct() 0 6 1
A getFirst() 0 7 2
A getIterator() 0 3 1
A loadStreams() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Info;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LogLevel;
9
use Psr\Log\NullLogger;
10
use Soluble\MediaTools\Video\Exception\InvalidStreamMetadataException;
11
use Soluble\MediaTools\Video\Exception\NoStreamException;
12
13
final class VideoStreamCollection implements VideoStreamCollectionInterface
14
{
15
    /** @var array<int, array> */
16
    private $streamsMetadata;
17
18
    /** @var array<int, VideoStreamInterface> */
19
    private $streams;
20
21
    /** @var LoggerInterface */
22
    private $logger;
23
24
    /**
25
     * @param array<int, array> $videoStreamsMetadata
26
     *
27
     * @throws InvalidStreamMetadataException
28
     */
29 10
    public function __construct(array $videoStreamsMetadata, ?LoggerInterface $logger = null)
30
    {
31 10
        $this->streamsMetadata = $videoStreamsMetadata;
32 10
        $this->logger          = $logger ?? new NullLogger();
33
34 10
        $this->loadStreams();
35 9
    }
36
37
    /**
38
     * @throws NoStreamException
39
     */
40 8
    public function getFirst(): VideoStreamInterface
41
    {
42 8
        if ($this->count() === 0) {
43 1
            throw new NoStreamException('Unable to get video first stream, none exists');
44
        }
45
46 7
        return new VideoStream($this->streamsMetadata[0]);
47
    }
48
49 9
    public function count(): int
50
    {
51 9
        return count($this->streamsMetadata);
52
    }
53
54
    /**
55
     * @return \ArrayIterator<int, VideoStreamInterface>
56
     */
57 2
    public function getIterator(): \ArrayIterator
58
    {
59 2
        return new \ArrayIterator($this->streams);
60
    }
61
62
    /**
63
     * @throws InvalidStreamMetadataException
64
     */
65 10
    private function loadStreams(): void
66
    {
67 10
        $this->streams = [];
68
69
        try {
70 10
            foreach ($this->streamsMetadata as $idx => $metadata) {
71 9
                if (!is_array($metadata)) {
72 1
                    throw new InvalidStreamMetadataException(sprintf(
73 1
                        'Invalid or unsupported metadata stream received %s',
74 1
                        (string) json_encode($metadata)
75
                    ));
76
                }
77 8
                $this->streams[] = new VideoStream($metadata);
78
            }
79 1
        } catch (InvalidStreamMetadataException $e) {
80 1
            $this->logger->log(LogLevel::ERROR, $e->getMessage());
81 1
            throw $e;
82
        }
83 9
    }
84
}
85