1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Cache\PhpFile\Adapter\FileSystem; |
4
|
|
|
|
5
|
|
|
use Hgraca\Cache\PhpFile\Port\FileSystem\Exception\FileNotFoundException as CacheFileNotFoundException; |
6
|
|
|
use Hgraca\Cache\PhpFile\Port\FileSystem\Exception\InvalidPathException as CacheInvalidPathException; |
7
|
|
|
use Hgraca\Cache\PhpFile\Port\FileSystem\Exception\PathAlreadyExistsException as CachePathAlreadyExistsException; |
8
|
|
|
use Hgraca\Cache\PhpFile\Port\FileSystem\FileSystemInterface as CacheFileSystemInterface; |
9
|
|
|
use Hgraca\FileSystem\Exception\FileNotFoundException; |
10
|
|
|
use Hgraca\FileSystem\Exception\InvalidPathException; |
11
|
|
|
use Hgraca\FileSystem\Exception\PathAlreadyExistsException; |
12
|
|
|
use Hgraca\FileSystem\FileSystemInterface; |
13
|
|
|
use Hgraca\FileSystem\LocalFileSystem; |
14
|
|
|
|
15
|
|
|
final class FileSystemAdapter implements CacheFileSystemInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var FileSystemInterface |
19
|
|
|
*/ |
20
|
|
|
private $fileSystem; |
21
|
|
|
|
22
|
23 |
|
public function __construct(FileSystemInterface $fileSystem = null) |
23
|
|
|
{ |
24
|
23 |
|
$this->fileSystem = $fileSystem ?? new LocalFileSystem(LocalFileSystem::IDEMPOTENT); |
25
|
23 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws CacheInvalidPathException |
29
|
|
|
*/ |
30
|
13 |
|
public function fileExists(string $path): bool |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
13 |
|
return $this->fileSystem->fileExists($path); |
34
|
1 |
|
} catch (InvalidPathException $e) { |
35
|
1 |
|
throw new CacheInvalidPathException('', 0, $e); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws CacheFileNotFoundException |
41
|
|
|
* @throws CacheInvalidPathException |
42
|
|
|
*/ |
43
|
4 |
|
public function readFile(string $path): string |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
4 |
|
return $this->fileSystem->readFile($path); |
47
|
2 |
|
} catch (InvalidPathException $e) { |
48
|
1 |
|
throw new CacheInvalidPathException('', 0, $e); |
49
|
1 |
|
} catch (FileNotFoundException $e) { |
50
|
1 |
|
throw new CacheFileNotFoundException('', 0, $e); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws CachePathAlreadyExistsException |
56
|
|
|
* @throws CacheInvalidPathException |
57
|
|
|
*/ |
58
|
7 |
|
public function writeFile(string $path, string $content) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
7 |
|
$this->fileSystem->writeFile($path, $content); |
62
|
2 |
|
} catch (InvalidPathException $e) { |
63
|
1 |
|
throw new CacheInvalidPathException('', 0, $e); |
64
|
1 |
|
} catch (PathAlreadyExistsException $e) { |
65
|
1 |
|
throw new CachePathAlreadyExistsException('', 0, $e); |
66
|
|
|
} |
67
|
5 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws CacheInvalidPathException |
71
|
|
|
*/ |
72
|
3 |
|
public function getFileCreationTimestamp(string $path): int |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
3 |
|
return $this->fileSystem->getFileCreationTimestamp($path); |
76
|
1 |
|
} catch (InvalidPathException $e) { |
77
|
1 |
|
throw new CacheInvalidPathException('', 0, $e); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|