|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\FileWaiter\Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
8
|
|
|
use Psr\Http\Message\StreamInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Stadly\FileWaiter\Adapter; |
|
11
|
|
|
use Stadly\FileWaiter\Exception\FileCouldNotBeFound; |
|
12
|
|
|
use Stadly\FileWaiter\Exception\StreamCouldNotBeOpened; |
|
13
|
|
|
use Stadly\Http\Header\Value\Date; |
|
14
|
|
|
use Stadly\Http\Header\Value\EntityTag\EntityTag; |
|
15
|
|
|
use Stadly\Http\Header\Value\MediaType\MediaType; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Adapter for handling files stored in the local file system. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Local implements Adapter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string Path to the file in the local file system. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $filePath; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var StreamFactoryInterface Factory for creating streams. |
|
29
|
|
|
*/ |
|
30
|
|
|
private $streamFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $filePath Path to the file in the local file system. |
|
36
|
|
|
* @param StreamFactoryInterface $streamFactory Factory for creating streams. |
|
37
|
|
|
* @throws FileCouldNotBeFound If the file does not exist. |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function __construct(string $filePath, StreamFactoryInterface $streamFactory) |
|
40
|
|
|
{ |
|
41
|
3 |
|
if (!is_file($filePath)) { |
|
42
|
2 |
|
throw new FileCouldNotBeFound($filePath); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$this->filePath = $filePath; |
|
46
|
1 |
|
$this->streamFactory = $streamFactory; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function getFileStream(): StreamInterface |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
2 |
|
return $this->streamFactory->createStreamFromFile($this->filePath); |
|
56
|
1 |
|
} catch (RuntimeException $exception) { |
|
57
|
1 |
|
throw new StreamCouldNotBeOpened($this->filePath, $exception); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function getFileName(): string |
|
65
|
|
|
{ |
|
66
|
3 |
|
return basename($this->filePath); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function getFileSize(): ?int |
|
73
|
|
|
{ |
|
74
|
|
|
// phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged |
|
75
|
2 |
|
$fileSize = @filesize($this->filePath); |
|
76
|
|
|
// phpcs:enable |
|
77
|
|
|
|
|
78
|
2 |
|
if ($fileSize === false) { |
|
79
|
1 |
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $fileSize; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function getMediaType(): ?MediaType |
|
89
|
|
|
{ |
|
90
|
|
|
// phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged |
|
91
|
3 |
|
$mediaTypeString = @mime_content_type($this->filePath); |
|
92
|
|
|
// phpcs:enable |
|
93
|
|
|
|
|
94
|
3 |
|
if ($mediaTypeString === false || $mediaTypeString === 'directory') { |
|
95
|
2 |
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return MediaType::fromString($mediaTypeString); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @inheritdoc |
|
103
|
|
|
*/ |
|
104
|
3 |
|
public function getLastModifiedDate(): ?Date |
|
105
|
|
|
{ |
|
106
|
|
|
// phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged |
|
107
|
3 |
|
$timestamp = @filemtime($this->filePath); |
|
108
|
|
|
// phpcs:enable |
|
109
|
|
|
|
|
110
|
3 |
|
if ($timestamp === false) { |
|
111
|
1 |
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
2 |
|
return Date::fromTimestamp($timestamp); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritdoc |
|
119
|
|
|
*/ |
|
120
|
2 |
|
public function getEntityTag(): ?EntityTag |
|
121
|
|
|
{ |
|
122
|
|
|
// phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged |
|
123
|
2 |
|
$entityTagString = @md5_file($this->filePath); |
|
124
|
|
|
// phpcs:enable |
|
125
|
|
|
|
|
126
|
2 |
|
if ($entityTagString === false) { |
|
127
|
1 |
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
return new EntityTag($entityTagString); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|