-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemImageSourceReader.php
More file actions
47 lines (36 loc) · 1.42 KB
/
FilesystemImageSourceReader.php
File metadata and controls
47 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Pdf;
use InvalidArgumentException;
/** @internal */
final readonly class FilesystemImageSourceReader implements FilesystemImageSourceReaderInterface
{
private WarningToExceptionConverterInterface $warningConverter;
public function __construct(?WarningToExceptionConverterInterface $warningConverter = null)
{
$this->warningConverter = $warningConverter ?? new PhpWarningToExceptionConverter();
}
public function read(string $source): string
{
$this->assertReadableSource($source);
$contents = $this->warningConverter->run(
static fn (): string|false => file_get_contents($source),
sprintf('Failed to read image source "%s".', $source),
);
if (!is_string($contents)) {
throw new InvalidArgumentException(sprintf('Failed to read image source "%s".', $source));
}
return $contents;
}
private function assertReadableSource(string $source): void
{
if (!is_file($source)) {
throw new InvalidArgumentException(sprintf('Image source "%s" must be an existing file.', $source));
}
if (!is_readable($source)) {
throw new InvalidArgumentException(sprintf('Image source "%s" must be readable.', $source));
}
}
}