Skip to content

Commit 6efc02b

Browse files
committed
File adapter interface
1 parent fb30166 commit 6efc02b

5 files changed

Lines changed: 129 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Updates should follow the [Keep a Changelog](http://keepachangelog.com/) princip
77
## Unreleased - YYYY-MM-DD
88

99
### Added
10-
- Nothing
10+
- File adapter interface.
1111

1212
### Deprecated
1313
- Nothing

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
}
2323
],
2424
"require": {
25-
"php": ">=7.4"
25+
"php": ">=7.4",
26+
"psr/http-message": "^1.0.1",
27+
"stadly/http": "^1.0"
2628
},
2729
"require-dev": {
2830
"pepakriz/phpstan-exception-rules": "^0.11.7",

src/Adapter.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stadly\FileWaiter;
6+
7+
use Psr\Http\Message\StreamInterface;
8+
use Stadly\FileWaiter\Exception\StreamCouldNotBeOpened;
9+
use Stadly\Http\Header\Value\Date;
10+
use Stadly\Http\Header\Value\EntityTag\EntityTag;
11+
use Stadly\Http\Header\Value\MediaType\MediaType;
12+
13+
/**
14+
* Interface for file adapters.
15+
*/
16+
interface Adapter
17+
{
18+
/**
19+
* @return StreamInterface File stream that can be used to read from the file.
20+
* @throws StreamCouldNotBeOpened If the file stream could not be opened.
21+
*/
22+
public function getFileStream(): StreamInterface;
23+
24+
/**
25+
* @return string|null File name, or null if not known.
26+
*/
27+
public function getFileName(): ?string;
28+
29+
/**
30+
* @return int|null File size, or null if not known.
31+
*/
32+
public function getFileSize(): ?int;
33+
34+
/**
35+
* @return MediaType|null Media type of the file, or null if not known.
36+
*/
37+
public function getMediaType(): ?MediaType;
38+
39+
/**
40+
* @return Date|null Date when the file was last modified, or null if not known.
41+
*/
42+
public function getLastModifiedDate(): ?Date;
43+
44+
/**
45+
* @return EntityTag|null Entity tag for the file, or null if not known.
46+
*/
47+
public function getEntityTag(): ?EntityTag;
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stadly\FileWaiter\Exception;
6+
7+
use RuntimeException;
8+
use Throwable;
9+
10+
/**
11+
* Exception thrown when a file stream could not be opened.
12+
*/
13+
final class StreamCouldNotBeOpened extends RuntimeException
14+
{
15+
/**
16+
* @var string Path to file.
17+
*/
18+
private $filePath;
19+
20+
/**
21+
* Constructor.
22+
*
23+
* @param string $filePath Path to file.
24+
* @param Throwable $previous Previous exception, used for exception chaining.
25+
*/
26+
public function __construct(string $filePath, ?Throwable $previous = null)
27+
{
28+
$this->filePath = $filePath;
29+
30+
parent::__construct('File stream could not be opened: ' . $filePath, /*code*/0, $previous);
31+
}
32+
33+
/**
34+
* @return string Path to file.
35+
*/
36+
public function getFilePath(): string
37+
{
38+
return $this->filePath;
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stadly\FileWaiter\Exception;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @coversDefaultClass \Stadly\FileWaiter\Exception\StreamCouldNotBeOpened
11+
* @covers ::<protected>
12+
* @covers ::<private>
13+
*/
14+
final class StreamCouldNotBeOpenedTest extends TestCase
15+
{
16+
/**
17+
* @covers ::__construct
18+
*/
19+
public function testCanConstructException(): void
20+
{
21+
$exception = new StreamCouldNotBeOpened('foo/bar.baz');
22+
23+
// Force generation of code coverage
24+
$exceptionConstruct = new StreamCouldNotBeOpened('foo/bar.baz');
25+
self::assertEquals($exception, $exceptionConstruct);
26+
}
27+
28+
/**
29+
* @covers ::getFilePath
30+
*/
31+
public function testCanGetFilePath(): void
32+
{
33+
$exception = new StreamCouldNotBeOpened('foo/bar.baz');
34+
35+
self::assertSame('foo/bar.baz', $exception->getFilePath());
36+
}
37+
}

0 commit comments

Comments
 (0)