|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hgraca\Lock\Adapter\FileSystem; |
|
4
|
|
|
|
|
5
|
|
|
use Hgraca\FileSystem\Exception\FileNotFoundException; |
|
6
|
|
|
use Hgraca\FileSystem\Exception\InvalidPathException; |
|
7
|
|
|
use Hgraca\FileSystem\Exception\PathAlreadyExistsException; |
|
8
|
|
|
use Hgraca\FileSystem\FileSystemInterface; |
|
9
|
|
|
use Hgraca\FileSystem\LocalFileSystem; |
|
10
|
|
|
use Hgraca\Lock\Port\FileSystem\Exception\FileNotFoundException as LockFileNotFoundException; |
|
11
|
|
|
use Hgraca\Lock\Port\FileSystem\Exception\InvalidPathException as LockInvalidPathException; |
|
12
|
|
|
use Hgraca\Lock\Port\FileSystem\Exception\PathAlreadyExistsException as LockPathAlreadyExistsException; |
|
13
|
|
|
use Hgraca\Lock\Port\FileSystem\FileSystemInterface as LockFileSystemInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class FileSystemAdapter implements LockFileSystemInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var FileSystemInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $fileSystem; |
|
21
|
|
|
|
|
22
|
22 |
|
public function __construct(FileSystemInterface $fileSystem = null) |
|
23
|
|
|
{ |
|
24
|
22 |
|
$this->fileSystem = $fileSystem ?? new LocalFileSystem(LocalFileSystem::IDEMPOTENT); |
|
25
|
22 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws LockInvalidPathException |
|
29
|
|
|
*/ |
|
30
|
11 |
|
public function fileExists(string $path): bool |
|
31
|
|
|
{ |
|
32
|
|
|
try { |
|
33
|
11 |
|
return $this->fileSystem->fileExists($path); |
|
34
|
1 |
|
} catch (InvalidPathException $e) { |
|
35
|
1 |
|
throw new LockInvalidPathException('', 0, $e); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws LockInvalidPathException |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function deleteFile(string $path): bool |
|
43
|
|
|
{ |
|
44
|
|
|
try { |
|
45
|
4 |
|
return $this->fileSystem->deleteFile($path); |
|
46
|
1 |
|
} catch (InvalidPathException $e) { |
|
47
|
1 |
|
throw new LockInvalidPathException('', 0, $e); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Creates a folder and all intermediate folders if they don't exist |
|
53
|
|
|
* |
|
54
|
|
|
* @throws LockInvalidPathException |
|
55
|
|
|
* @throws LockPathAlreadyExistsException |
|
56
|
|
|
*/ |
|
57
|
8 |
View Code Duplication |
public function createDir(string $path): bool |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
8 |
|
return $this->fileSystem->createDir($path); |
|
61
|
2 |
|
} catch (InvalidPathException $e) { |
|
62
|
1 |
|
throw new LockInvalidPathException('', 0, $e); |
|
63
|
1 |
|
} catch (PathAlreadyExistsException $e) { |
|
64
|
1 |
|
throw new LockPathAlreadyExistsException('', 0, $e); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @throws LockFileNotFoundException |
|
70
|
|
|
* @throws LockInvalidPathException |
|
71
|
|
|
*/ |
|
72
|
9 |
|
public function readFile(string $path): string |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
9 |
|
return $this->fileSystem->readFile($path); |
|
76
|
3 |
|
} catch (InvalidPathException $e) { |
|
77
|
1 |
|
throw new LockInvalidPathException('', 0, $e); |
|
78
|
2 |
|
} catch (FileNotFoundException $e) { |
|
79
|
1 |
|
throw new LockFileNotFoundException('', 0, $e); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @throws LockPathAlreadyExistsException |
|
85
|
|
|
* @throws LockInvalidPathException |
|
86
|
|
|
*/ |
|
87
|
8 |
View Code Duplication |
public function writeFile(string $path, string $content) |
|
88
|
|
|
{ |
|
89
|
|
|
try { |
|
90
|
8 |
|
$this->fileSystem->writeFile($path, $content); |
|
91
|
2 |
|
} catch (InvalidPathException $e) { |
|
92
|
1 |
|
throw new LockInvalidPathException('', 0, $e); |
|
93
|
1 |
|
} catch (PathAlreadyExistsException $e) { |
|
94
|
1 |
|
throw new LockPathAlreadyExistsException('', 0, $e); |
|
95
|
|
|
} |
|
96
|
6 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|