|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Archivr\ConnectionAdapter; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Filesystem; |
|
6
|
|
|
|
|
7
|
|
|
class FlysystemConnectionAdapter implements ConnectionAdapterInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Filesystem |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $filesystem; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Filesystem $filesystem) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->filesystem = $filesystem; |
|
17
|
|
|
$this->filesystem->getConfig()->set('disable_asserts', true); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function read(string $relativePath): string |
|
21
|
|
|
{ |
|
22
|
|
|
$content = $this->filesystem->read($relativePath); |
|
23
|
|
|
|
|
24
|
|
|
if (!is_string($content)) |
|
25
|
|
|
{ |
|
26
|
|
|
throw new \RuntimeException(sprintf('read() failed for %s.', $relativePath)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $content; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
View Code Duplication |
public function write(string $relativePath, string $content) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$success = $this->filesystem->write($relativePath, $content); |
|
35
|
|
|
|
|
36
|
|
|
if (!$success) |
|
37
|
|
|
{ |
|
38
|
|
|
throw new \RuntimeException(sprintf('write() failed for %s.', $relativePath)); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function writeStream(string $relativePath, $stream) |
|
43
|
|
|
{ |
|
44
|
|
|
$success = $this->filesystem->writeStream($relativePath, $stream); |
|
45
|
|
|
|
|
46
|
|
|
if (!$success) |
|
47
|
|
|
{ |
|
48
|
|
|
throw new \RuntimeException(sprintf('writeStream() failed for %s.', $relativePath)); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function exists(string $relativePath): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->filesystem->has($relativePath); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function unlink(string $relativePath) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$success = $this->filesystem->delete($relativePath); |
|
60
|
|
|
|
|
61
|
|
|
if (!$success) |
|
62
|
|
|
{ |
|
63
|
|
|
throw new \RuntimeException(sprintf('unlink() failed for %s', $relativePath)); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getReadStream(string $relativePath) |
|
68
|
|
|
{ |
|
69
|
|
|
$stream = $this->filesystem->readStream($relativePath); |
|
70
|
|
|
|
|
71
|
|
|
if (!is_resource($stream)) |
|
72
|
|
|
{ |
|
73
|
|
|
throw new \RuntimeException(sprintf('getReadStream() failed for %s.', $relativePath)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $stream; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
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.