1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Storeman\StorageAdapter; |
4
|
|
|
|
5
|
|
|
use Storeman\Exception; |
6
|
|
|
use League\Flysystem\Filesystem; |
7
|
|
|
|
8
|
|
|
abstract class FlysystemStorageAdapter extends AbstractStorageAdapter |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Filesystem |
12
|
|
|
*/ |
13
|
|
|
protected $filesystem; |
14
|
|
|
|
15
|
|
|
public function __construct(Filesystem $filesystem) |
16
|
|
|
{ |
17
|
|
|
parent::__construct(); |
18
|
|
|
|
19
|
|
|
$this->filesystem = $filesystem; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function writeStream(string $relativePath, $stream) |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->logger->debug("Writing stream to {$relativePath}..."); |
25
|
|
|
|
26
|
|
|
try |
27
|
|
|
{ |
28
|
|
|
$success = $this->filesystem->putStream($relativePath, $stream); |
29
|
|
|
|
30
|
|
|
if (!$success) |
31
|
|
|
{ |
32
|
|
|
throw new Exception(sprintf('writeStream() failed for %s.', $relativePath)); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
catch (\Exception $exception) |
36
|
|
|
{ |
37
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function exists(string $relativePath): bool |
42
|
|
|
{ |
43
|
|
|
try |
44
|
|
|
{ |
45
|
|
|
return $this->filesystem->has($relativePath); |
46
|
|
|
} |
47
|
|
|
catch (\Exception $exception) |
48
|
|
|
{ |
49
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function unlink(string $relativePath) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->logger->debug("Deleting {$relativePath}..."); |
56
|
|
|
|
57
|
|
|
try |
58
|
|
|
{ |
59
|
|
|
$success = $this->filesystem->delete($relativePath); |
60
|
|
|
|
61
|
|
|
if (!$success) |
62
|
|
|
{ |
63
|
|
|
throw new Exception(sprintf('unlink() failed for %s', $relativePath)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
catch (\Exception $exception) |
67
|
|
|
{ |
68
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function getReadStream(string $relativePath) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->logger->debug("Getting read stream for {$relativePath}..."); |
75
|
|
|
|
76
|
|
|
try |
77
|
|
|
{ |
78
|
|
|
$stream = $this->filesystem->readStream($relativePath); |
79
|
|
|
|
80
|
|
|
if (!is_resource($stream)) |
81
|
|
|
{ |
82
|
|
|
throw new Exception(sprintf('getReadStream() failed for %s.', $relativePath)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $stream; |
86
|
|
|
} |
87
|
|
|
catch (\Exception $exception) |
88
|
|
|
{ |
89
|
|
|
throw new Exception($exception->getMessage(), 0, $exception); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.