Passed
Push — master ( b38e7e...085481 )
by Sébastien
03:00
created

MetadataTypeSafeReader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 56.82%

Importance

Changes 0
Metric Value
wmc 14
eloc 44
dl 0
loc 117
c 0
b 0
f 0
ccs 25
cts 44
cp 0.5682
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyStringOrNullValue() 0 15 3
A getKeyIntValue() 0 12 2
A getKeyFloatOrNullValue() 0 15 3
A getKeyIntOrNullValue() 0 15 3
A getKeyFloatValue() 0 12 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Info\Util;
6
7
use Soluble\MediaTools\Video\Exception\UnexpectedMetadataException;
8
use Webmozart\Assert\Assert;
9
10
class MetadataTypeSafeReader
11
{
12
    /**
13
     * @var array<string, mixed>
14
     */
15
    private $streamMetadata;
16
17
    /**
18
     * @param array<string, mixed> $streamMetadata
19
     */
20 1
    public function __construct(array $streamMetadata)
21
    {
22 1
        $this->streamMetadata = $streamMetadata;
23 1
    }
24
25
    /**
26
     * @param string $key metadata key
27
     *
28
     * @throws UnexpectedMetadataException
29
     */
30 1
    public function getKeyIntValue(string $key): int
31
    {
32
        try {
33 1
            Assert::integerish(
34 1
                $this->streamMetadata[$key] ?? '',
35 1
                "The ffprobe/videoInfo metadata '$key' is expected to be an integer. Got: %s"
36
            );
37
        } catch (\Throwable $e) {
38
            throw new UnexpectedMetadataException($e->getMessage());
39
        }
40
41 1
        return (int) $this->streamMetadata[$key];
42
    }
43
44
    /**
45
     * @param string $key metadata key
46
     *
47
     * @throws UnexpectedMetadataException
48
     */
49 1
    public function getKeyIntOrNullValue(string $key): ?int
50
    {
51
        try {
52 1
            Assert::nullOrIntegerish(
53 1
                $this->streamMetadata[$key] ?? null,
54 1
                "The ffprobe/videoInfo metadata '$key' is expected to be an integer or null. Got: %s"
55
            );
56
        } catch (\Throwable $e) {
57
            throw new UnexpectedMetadataException($e->getMessage());
58
        }
59 1
        if (isset($this->streamMetadata[$key])) {
60 1
            return (int) $this->streamMetadata[$key];
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * @param string $key metadata key
68
     *
69
     * @throws UnexpectedMetadataException
70
     */
71 1
    public function getKeyFloatValue(string $key): float
72
    {
73
        try {
74 1
            Assert::numeric(
75 1
                $this->streamMetadata[$key] ?? '',
76 1
                "The ffprobe/videoInfo metadata '$key' is expected to be a float. Got: %s"
77
            );
78
        } catch (\Throwable $e) {
79
            throw new UnexpectedMetadataException($e->getMessage());
80
        }
81
82 1
        return (float) $this->streamMetadata[$key];
83
    }
84
85
    /**
86
     * @param string $key metadata key
87
     *
88
     * @throws UnexpectedMetadataException
89
     */
90
    public function getKeyFloatOrNullValue(string $key): ?float
91
    {
92
        try {
93
            Assert::nullOrNumeric(
94
                $this->streamMetadata[$key] ?? null,
95
                "The ffprobe/videoInfo metadata '$key' is expected to be a float or null. Got: %s"
96
            );
97
        } catch (\Throwable $e) {
98
            throw new UnexpectedMetadataException($e->getMessage());
99
        }
100
        if (isset($this->streamMetadata[$key])) {
101
            return (float) $this->streamMetadata[$key];
102
        }
103
104
        return null;
105
    }
106
107
    /**
108
     * @param string $key metadata key
109
     *
110
     * @throws UnexpectedMetadataException
111
     */
112 1
    public function getKeyStringOrNullValue(string $key): ?string
113
    {
114
        try {
115 1
            Assert::nullOrString(
116 1
                $this->streamMetadata[$key] ?? null,
117 1
                "The ffprobe/videoInfo metadata '$key' is expected to be a string or null. Got: %s"
118
            );
119
        } catch (\Throwable $e) {
120
            throw new UnexpectedMetadataException($e->getMessage());
121
        }
122 1
        if (isset($this->streamMetadata[$key])) {
123 1
            return (string) $this->streamMetadata[$key];
124
        }
125
126
        return null;
127
    }
128
}
129