1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Archivr\StorageAdapter; |
4
|
|
|
|
5
|
|
|
use Archivr\Exception\Exception; |
6
|
|
|
use League\Flysystem\Filesystem; |
7
|
|
|
|
8
|
|
|
abstract class FlysystemStorageAdapter implements StorageAdapterInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Filesystem |
12
|
|
|
*/ |
13
|
|
|
protected $filesystem; |
14
|
|
|
|
15
|
|
|
public function __construct(Filesystem $filesystem) |
16
|
|
|
{ |
17
|
|
|
$this->filesystem = $filesystem; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function read(string $relativePath): string |
21
|
|
|
{ |
22
|
|
|
try |
23
|
|
|
{ |
24
|
|
|
$content = $this->filesystem->read($relativePath); |
25
|
|
|
|
26
|
|
|
if (!is_string($content)) |
27
|
|
|
{ |
28
|
|
|
throw new Exception(sprintf('read() failed for %s.', $relativePath)); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
catch (\Exception $exception) |
32
|
|
|
{ |
33
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $content; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function write(string $relativePath, string $content) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
try |
42
|
|
|
{ |
43
|
|
|
$success = $this->filesystem->put($relativePath, $content); |
44
|
|
|
|
45
|
|
|
if (!$success) |
46
|
|
|
{ |
47
|
|
|
throw new Exception(sprintf('write() failed for %s.', $relativePath)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
catch (\Exception $exception) |
51
|
|
|
{ |
52
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function writeStream(string $relativePath, $stream) |
57
|
|
|
{ |
58
|
|
|
try |
59
|
|
|
{ |
60
|
|
|
$success = $this->filesystem->putStream($relativePath, $stream); |
61
|
|
|
|
62
|
|
|
if (!$success) |
63
|
|
|
{ |
64
|
|
|
throw new Exception(sprintf('writeStream() failed for %s.', $relativePath)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
catch (\Exception $exception) |
68
|
|
|
{ |
69
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function exists(string $relativePath): bool |
74
|
|
|
{ |
75
|
|
|
try |
76
|
|
|
{ |
77
|
|
|
return $this->filesystem->has($relativePath); |
78
|
|
|
} |
79
|
|
|
catch (\Exception $exception) |
80
|
|
|
{ |
81
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function unlink(string $relativePath) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
try |
88
|
|
|
{ |
89
|
|
|
$success = $this->filesystem->delete($relativePath); |
90
|
|
|
|
91
|
|
|
if (!$success) |
92
|
|
|
{ |
93
|
|
|
throw new Exception(sprintf('unlink() failed for %s', $relativePath)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
catch (\Exception $exception) |
97
|
|
|
{ |
98
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function getReadStream(string $relativePath) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
try |
105
|
|
|
{ |
106
|
|
|
$stream = $this->filesystem->readStream($relativePath); |
107
|
|
|
|
108
|
|
|
if (!is_resource($stream)) |
109
|
|
|
{ |
110
|
|
|
throw new Exception(sprintf('getReadStream() failed for %s.', $relativePath)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $stream; |
114
|
|
|
} |
115
|
|
|
catch (\Exception $exception) |
116
|
|
|
{ |
117
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.