1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Venta\Filesystem; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\FilesystemInterface; |
6
|
|
|
use Venta\Contracts\Filesystem\Filesystem as FilesystemContract; |
7
|
|
|
use Venta\Contracts\Filesystem\Metadata as MetadataContract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Filesystem |
11
|
|
|
* |
12
|
|
|
* @package Venta\Filesystem |
13
|
|
|
*/ |
14
|
|
|
class Filesystem implements FilesystemContract |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var FilesystemInterface |
19
|
|
|
*/ |
20
|
|
|
private $flysystem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Filesystem constructor. |
24
|
|
|
* |
25
|
|
|
* @param FilesystemInterface $flysystem |
26
|
|
|
*/ |
27
|
6 |
|
public function __construct(FilesystemInterface $flysystem) |
28
|
|
|
{ |
29
|
6 |
|
$this->flysystem = $flysystem; |
30
|
6 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
*/ |
35
|
1 |
|
public function append(string $path, string $contents, array $config = []): bool |
36
|
|
|
{ |
37
|
1 |
|
if ($this->exists($path)) { |
38
|
1 |
|
$contents = $this->read($path) . $contents; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $this->write($path, $contents); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
1 |
|
public function copy(string $path, string $newPath): bool |
48
|
|
|
{ |
49
|
1 |
|
return $this->flysystem->copy($path, $newPath); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
1 |
|
public function createDir(string $dirname, array $config = []): bool |
56
|
|
|
{ |
57
|
1 |
|
return $this->flysystem->createDir($dirname, $config); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
1 |
|
public function delete(string $path): bool |
64
|
|
|
{ |
65
|
1 |
|
return $this->flysystem->delete($path); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
1 |
|
public function deleteDir(string $dirname): bool |
72
|
|
|
{ |
73
|
1 |
|
return $this->flysystem->deleteDir($dirname); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
2 |
|
public function exists(string $path): bool |
80
|
|
|
{ |
81
|
2 |
|
return $this->flysystem->has($path); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritDoc |
86
|
|
|
*/ |
87
|
|
|
public function list(string $path = '.', bool $recursive = false): array |
88
|
|
|
{ |
89
|
1 |
|
return array_map( |
90
|
|
|
function($item){ return new Metadata($item); }, |
91
|
1 |
|
$this->flysystem->listContents($path, $recursive) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
1 |
View Code Duplication |
public function listDirectories(string $path = '.', bool $recursive = false): array |
|
|
|
|
99
|
|
|
{ |
100
|
1 |
|
return array_map( |
101
|
|
|
function($item){ return new Metadata($item); }, |
102
|
|
|
array_filter( |
103
|
1 |
|
$this->flysystem->listContents($path, $recursive), |
104
|
|
|
function($item){ return $item['type'] === MetadataContract::TYPE_DIR; } |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
1 |
View Code Duplication |
public function listFiles(string $path = '.', bool $recursive = false): array |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
return array_map( |
115
|
|
|
function($item){ return new Metadata($item); }, |
116
|
|
|
array_filter( |
117
|
1 |
|
$this->flysystem->listContents($path, $recursive), |
118
|
|
|
function($item){ return $item['type'] === MetadataContract::TYPE_FILE; } |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
1 |
|
public function metadata($path): MetadataContract |
127
|
|
|
{ |
128
|
1 |
|
return new Metadata($this->flysystem->getMetadata($path)); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
1 |
|
public function move(string $path, string $newPath): bool |
135
|
|
|
{ |
136
|
1 |
|
return $this->flysystem->rename($path, $newPath); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
1 |
|
public function prepend(string $path, string $contents, array $config = []): bool |
143
|
|
|
{ |
144
|
1 |
|
if ($this->exists($path)) { |
145
|
1 |
|
$contents .= $this->read($path); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return $this->write($path, $contents); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
2 |
|
public function read(string $path): string |
155
|
|
|
{ |
156
|
2 |
|
return $this->flysystem->read($path); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritDoc |
161
|
|
|
*/ |
162
|
2 |
|
public function write(string $path, string $contents, array $config = []): bool |
163
|
|
|
{ |
164
|
2 |
|
return $this->flysystem->put($path, $contents, $config); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
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.