-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFilesystemExceptionHandler.php
More file actions
66 lines (48 loc) · 1.71 KB
/
FilesystemExceptionHandler.php
File metadata and controls
66 lines (48 loc) · 1.71 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace CodeRhapsodie\DataflowBundle\ExceptionsHandler;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
class FilesystemExceptionHandler implements ExceptionHandlerInterface
{
public function __construct(private readonly Filesystem $filesystem)
{
}
public function save(?int $jobId, $exceptions): void
{
if ($jobId === null || !\is_resource($exceptions) || stream_get_contents($exceptions, 1) === false) {
return;
}
$path = \sprintf('dataflow-job-%s.log', $jobId);
rewind($exceptions);
if ($this->filesystem->fileExists($path)) {
$existingStream = $this->filesystem->readStream($path);
$combined = fopen('php://temp', 'r+');
stream_copy_to_stream($existingStream, $combined);
stream_copy_to_stream($exceptions, $combined);
rewind($combined);
$this->filesystem->delete($path);
$this->filesystem->writeStream($path, $combined);
fclose($existingStream);
fclose($combined);
return;
}
$this->filesystem->writeStream($path, $exceptions);
fclose($exceptions);
}
public function find(int $jobId)
{
try {
if (!$this->filesystem->fileExists(\sprintf('dataflow-job-%s.log', $jobId))) {
return null;
}
return $this->filesystem->readStream(\sprintf('dataflow-job-%s.log', $jobId));
} catch (FilesystemException) {
return null;
}
}
public function delete(int $jobId): void
{
$this->filesystem->delete(\sprintf('dataflow-job-%s.log', $jobId));
}
}