MetadataTypeSafeReader   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
eloc 63
c 1
b 0
f 1
dl 0
loc 152
ccs 68
cts 68
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyStringValue() 0 13 2
A getKeyStringOrNullValue() 0 17 4
A getKeyIntValue() 0 14 2
A getKeyFloatOrNullValue() 0 19 4
A getKeyIntOrNullValue() 0 17 4
A getKeyFloatValue() 0 15 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Info\Util;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use Soluble\MediaTools\Video\Exception\UnexpectedMetadataException;
10
11
class MetadataTypeSafeReader
12
{
13
    /** @var array<string, mixed> */
14
    private $streamMetadata;
15
16
    /** @var LoggerInterface */
17
    private $logger;
18
19
    /**
20
     * @param array<string, mixed> $streamMetadata
21
     */
22 21
    public function __construct(array $streamMetadata, ?LoggerInterface $logger = null)
23
    {
24 21
        $this->streamMetadata = $streamMetadata;
25 21
        $this->logger         = $logger ?? new NullLogger();
26 21
    }
27
28
    /**
29
     * @param string $key metadata key
30
     *
31
     * @throws UnexpectedMetadataException
32
     */
33 10
    public function getKeyIntValue(string $key): int
34
    {
35 10
        $value = $this->streamMetadata[$key] ?? '<empty>';
36 10
        if (filter_var($value, FILTER_VALIDATE_INT) === false) {
37 1
            $msg = sprintf(
38 1
                "The ffprobe/videoInfo metadata '$key' is expected to be an integer. Got: %s (%s)",
39 1
                gettype($value),
40 1
                (string) $value
41
            );
42 1
            $this->logger->notice($msg);
43 1
            throw new UnexpectedMetadataException($msg);
44
        }
45
46 9
        return (int) $this->streamMetadata[$key];
47
    }
48
49
    /**
50
     * @param string $key metadata key
51
     *
52
     * @throws UnexpectedMetadataException
53
     */
54 10
    public function getKeyIntOrNullValue(string $key): ?int
55
    {
56 10
        $value = $this->streamMetadata[$key] ?? null;
57 10
        if ($value !== null && filter_var($value, FILTER_VALIDATE_INT) === false) {
58 1
            $msg = sprintf(
59 1
                "The ffprobe/videoInfo metadata '$key' is expected to be an integer or null. Got: %s (%s)",
60 1
                gettype($value),
61 1
                (string) $value
62
            );
63 1
            $this->logger->notice($msg);
64 1
            throw new UnexpectedMetadataException($msg);
65
        }
66 9
        if (isset($this->streamMetadata[$key])) {
67 7
            return (int) $this->streamMetadata[$key];
68
        }
69
70 5
        return null;
71
    }
72
73
    /**
74
     * @param string $key metadata key
75
     *
76
     * @throws UnexpectedMetadataException
77
     */
78 5
    public function getKeyFloatValue(string $key): float
79
    {
80 5
        $value = $this->streamMetadata[$key] ?? '<empty>';
81 5
        if (!is_numeric($value)) {
82 2
            $msg = sprintf(
83 2
                "The ffprobe/videoInfo metadata '$key' is expected to be a float. Got: %s (%s)",
84 2
                gettype($value),
85 2
                (string) $value
86
            );
87 2
            $this->logger->notice($msg);
88
89 2
            throw new UnexpectedMetadataException($msg);
90
        }
91
92 4
        return (float) $this->streamMetadata[$key];
93
    }
94
95
    /**
96
     * @param string $key metadata key
97
     *
98
     * @throws UnexpectedMetadataException
99
     */
100 5
    public function getKeyFloatOrNullValue(string $key): ?float
101
    {
102 5
        $value = $this->streamMetadata[$key] ?? null;
103 5
        if ($value !== null && !is_numeric($value)) {
104 1
            $msg = sprintf(
105 1
                "The ffprobe/videoInfo metadata '$key' is expected to be a float or null. Got: %s (%s)",
106 1
                gettype($value),
107 1
                (string) $value
108
            );
109 1
            $this->logger->notice($msg);
110
111 1
            throw new UnexpectedMetadataException($msg);
112
        }
113
114 4
        if (isset($this->streamMetadata[$key])) {
115 3
            return (float) $this->streamMetadata[$key];
116
        }
117
118 2
        return null;
119
    }
120
121
    /**
122
     * @param string $key metadata key
123
     *
124
     * @throws UnexpectedMetadataException
125
     */
126 8
    public function getKeyStringOrNullValue(string $key): ?string
127
    {
128 8
        $value = $this->streamMetadata[$key] ?? null;
129 8
        if ($value !== null && !is_scalar($value)) {
130 1
            $msg = sprintf(
131 1
                "The ffprobe/videoInfo metadata '$key' is expected to be a string or null. Got: %s",
132 1
                gettype($value)
133
            );
134 1
            $this->logger->notice($msg);
135 1
            throw new UnexpectedMetadataException($msg);
136
        }
137
138 7
        if (isset($this->streamMetadata[$key])) {
139 7
            return (string) $this->streamMetadata[$key];
140
        }
141
142 3
        return null;
143
    }
144
145
    /**
146
     * @param string $key metadata key
147
     *
148
     * @throws UnexpectedMetadataException
149
     */
150 10
    public function getKeyStringValue(string $key): string
151
    {
152 10
        $value = $this->streamMetadata[$key] ?? null;
153 10
        if (!is_scalar($value)) {
154 1
            $msg = sprintf(
155 1
                "The ffprobe/videoInfo metadata '$key' is expected to be a string. Got: %s",
156 1
                gettype($value)
157
            );
158 1
            $this->logger->notice($msg);
159 1
            throw new UnexpectedMetadataException($msg);
160
        }
161
162 9
        return (string) $this->streamMetadata[$key];
163
    }
164
}
165