Completed
Push — master ( 3a69b5...ff1cb1 )
by Aimeos
03:21
created

lib/custom/src/MW/Filesystem/FlyBase.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package MW
7
 * @subpackage Filesystem
8
 */
9
10
11
namespace Aimeos\MW\Filesystem;
12
13
14
/**
15
 * Implementation of Flysystem file system adapter
16
 *
17
 * @package MW
18
 * @subpackage Filesystem
19
 */
20
abstract class FlyBase implements Iface, DirIface, MetaIface
21
{
22
	private $config;
23
	private $tempdir;
24
25
26
	/**
27
	 * Initializes the object
28
	 *
29
	 * @param array $config Configuration options
30
	 */
31
	public function __construct( array $config )
32
	{
33
		$this->config = $config;
34
35
		if( !isset( $config['tempdir'] ) ) {
36
			$config['tempdir'] = sys_get_temp_dir();
37
		}
38
39
		if( !is_dir( $config['tempdir'] ) && mkdir( $config['tempdir'], 0755, true ) === false ) {
40
			throw new Exception( sprintf( 'Directory "%1$s" could not be created', $config['tempdir'] ) );
41
		}
42
43
		$ds = DIRECTORY_SEPARATOR;
44
		$this->tempdir = realpath( str_replace( '/', $ds, rtrim( $config['tempdir'], '/' ) ) ) . $ds;
45
	}
46
47
48
	/**
49
	 * Tests if the given path is a directory
50
	 *
51
	 * @param string $path Path to the file or directory
52
	 * @return boolean True if directory, false if not
53
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
54
	 */
55
	public function isdir( $path )
56
	{
57
		$result = $this->getProvider()->getMetadata( $path );
58
59
		if( $result['type'] === 'dir' ) {
60
			return true;
61
		}
62
63
		return false;
64
	}
65
66
67
	/**
68
	 * Creates a new directory for the given path
69
	 *
70
	 * @param string $path Path to the directory
71
	 * @return void
72
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
73
	 */
74
	public function mkdir( $path )
75
	{
76
		if( $this->getProvider()->createDir( $path ) === false ) {
77
			throw new Exception( $path );
78
		}
79
	}
80
81
82
	/**
83
	 * Deletes the directory for the given path
84
	 *
85
	 * @param string $path Path to the directory
86
	 * @return void
87
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
88
	 */
89
	public function rmdir( $path )
90
	{
91
		if( $this->getProvider()->deleteDir( $path ) === false ) {
92
			throw new Exception( $path );
93
		}
94
	}
95
96
97
	/**
98
	 * Returns an iterator over the entries in the given path
99
	 *
100
	 * {@inheritDoc}
101
	 *
102
	 * @param string $path Path to the filesystem or directory
103
	 * @return \Iterator|array Iterator over the entries or array with entries
104
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
105
	 */
106
	public function scan( $path = null )
107
	{
108
		$list = array();
109
110
		foreach( $this->getProvider()->listContents( $path ) as $entry ) {
111
			$list[] = $entry['basename'];
112
		}
113
114
		return $list;
115
	}
116
117
118
	/**
119
	 * Returns the file size
120
	 *
121
	 * @param string $path Path to the file
122
	 * @return integer Size in bytes
123
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
124
	 */
125 View Code Duplication
	public function size( $path )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
126
	{
127
		try {
128
			$size = $this->getProvider()->getSize( $path );
129
		} catch( \Exception $e ) {
130
			throw new Exception( $e->getMessage(), 0, $e );
131
		}
132
133
		if( $size === false ) {
134
			throw new Exception( $path );
135
		}
136
137
		return $size;
138
	}
139
140
141
	/**
142
	 * Returns the Unix time stamp for the file
143
	 *
144
	 * @param string $path Path to the file
145
	 * @return integer Unix time stamp in seconds
146
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
147
	 */
148 View Code Duplication
	public function time( $path )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
149
	{
150
		try {
151
			$time = $this->getProvider()->getTimestamp( $path );
152
		} catch( \Exception $e ) {
153
			throw new Exception( $e->getMessage(), 0, $e );
154
		}
155
156
		if( $time === false ) {
157
			throw new Exception( $path );
158
		}
159
160
		return $time;
161
	}
162
163
164
	/**
165
	 * Deletes the file for the given path
166
	 *
167
	 * @param string $path Path to the file
168
	 * @return void
169
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
170
	 */
171
	public function rm( $path )
172
	{
173
		try {
174
			$this->getProvider()->delete( $path );
175
		} catch( \Exception $e ) {
176
			throw new Exception( $e->getMessage(), 0, $e );
177
		}
178
	}
179
180
181
	/**
182
	 * Tests if a file exists at the given path
183
	 *
184
	 * @param string $path Path to the file
185
	 * @return boolean True if it exists, false if not
186
	 */
187
	public function has( $path )
188
	{
189
		return $this->getProvider()->has( $path );
190
	}
191
192
193
	/**
194
	 * Returns the content of the file
195
	 *
196
	 * {@inheritDoc}
197
	 *
198
	 * @param string $path Path to the file
199
	 * @return string File content
200
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
201
	 */
202 View Code Duplication
	public function read( $path )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
203
	{
204
		try {
205
			$content = $this->getProvider()->read( $path );
206
		} catch( \Exception $e ) {
207
			throw new Exception( $e->getMessage(), 0, $e );
208
		}
209
210
		if( $content === false ) {
211
			throw new Exception( $path );
212
		}
213
214
		return $content;
215
	}
216
217
218
	/**
219
	 * Reads the content of the remote file and writes it to a local one
220
	 *
221
	 * @param string $path Path to the remote file
222
	 * @return string Path of the local file
223
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
224
	 */
225
	public function readf( $path )
226
	{
227
		if( ( $filename = tempnam( $this->tempdir, 'ai-' ) ) === false ) {
228
			throw new Exception( sprintf( 'Unable to create file in "%1$s"', $this->tempdir ) );
229
		}
230
231
		if( ( $handle = @fopen( $filename, 'w' ) ) === false ) {
232
			throw new Exception( sprintf( 'Unable to open file "%1$s"', $filename ) );
233
		}
234
235
		$stream = $this->reads( $path );
236
237
		if( @stream_copy_to_stream( $stream, $handle ) == false ) {
238
			throw new Exception( sprintf( 'Couldn\'t copy stream for "%1$s"', $path ) );
239
		}
240
241
		fclose( $stream );
242
		fclose( $handle );
243
244
		return $filename;
245
	}
246
247
248
	/**
249
	 * Returns the stream descriptor for the file
250
	 *
251
	 * {@inheritDoc}
252
	 *
253
	 * @param string $path Path to the file
254
	 * @return resource File stream descriptor
255
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
256
	 */
257 View Code Duplication
	public function reads( $path )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
258
	{
259
		try {
260
			$handle = $this->getProvider()->readStream( $path );
261
		} catch( \Exception $e ) {
262
			throw new Exception( $e->getMessage(), 0, $e );
263
		}
264
265
		if( $handle === false ) {
266
			throw new Exception( $path );
267
		}
268
269
		return $handle;
270
	}
271
272
273
	/**
274
	 * Writes the given content to the file
275
	 *
276
	 * {@inheritDoc}
277
	 *
278
	 * @param string $path Path to the file
279
	 * @param string $content New file content
280
	 * @return void
281
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
282
	 */
283 View Code Duplication
	public function write( $path, $content )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
284
	{
285
		try {
286
			$result = $this->getProvider()->put( $path, $content );
287
		} catch( \Exception $e ) {
288
			throw new Exception( $e->getMessage(), 0, $e );
289
		}
290
291
		if( $result === false ) {
292
			throw new Exception( $path );
293
		}
294
	}
295
296
297
	/**
298
	 * Writes the content of the local file to the remote path
299
	 *
300
	 * {@inheritDoc}
301
	 *
302
	 * @param string $path Path to the remote file
303
	 * @param string $file Path to the local file
0 ignored issues
show
There is no parameter named $file. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
304
	 * @return void
305
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
306
	 */
307
	public function writef( $path, $local )
308
	{
309
		if( ( $handle = @fopen( $local, 'r' ) ) === false ) {
310
			throw new Exception( sprintf( 'Unable to open file "%1$s"', $local ) );
311
		}
312
313
		$this->writes( $path, $handle );
314
315
		if( is_resource( $handle ) ) {
316
			fclose( $handle );
317
		}
318
	}
319
320
321
	/**
322
	 * Write the content of the stream descriptor into the remote file
323
	 *
324
	 * {@inheritDoc}
325
	 *
326
	 * @param string $path Path to the file
327
	 * @param resource $stream File stream descriptor
328
	 * @return void
329
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
330
	 */
331 View Code Duplication
	public function writes( $path, $stream )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
332
	{
333
		try {
334
			$result = $this->getProvider()->putStream( $path, $stream );
335
		} catch( \Exception $e ) {
336
			throw new Exception( $e->getMessage(), 0, $e );
337
		}
338
339
		if( $result === false ) {
340
			throw new Exception( $path );
341
		}
342
	}
343
344
345
	/**
346
	 * Renames a file, moves it to a new location or both at once
347
	 *
348
	 * @param string $from Path to the original file
349
	 * @param string $to Path to the new file
350
	 * @return void
351
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
352
	 */
353 View Code Duplication
	public function move( $from, $to )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
354
	{
355
		try {
356
			$result = $this->getProvider()->rename( $from, $to );
357
		} catch( \Exception $e ) {
358
			throw new Exception( $e->getMessage(), 0, $e );
359
		}
360
361
		if( $result === false ) {
362
			throw new Exception( sprintf( 'Error moving "%1$s" to "%2$s"', $from, $to ) );
363
		}
364
	}
365
366
367
	/**
368
	 * Copies a file to a new location
369
	 *
370
	 * @param string $from Path to the original file
371
	 * @param string $to Path to the new file
372
	 * @return void
373
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
374
	 */
375 View Code Duplication
	public function copy( $from, $to )
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
376
	{
377
		try {
378
			$result = $this->getProvider()->copy( $from, $to );
379
		} catch( \Exception $e ) {
380
			throw new Exception( $e->getMessage(), 0, $e );
381
		}
382
383
		if( $result === false ) {
384
			throw new Exception( sprintf( 'Error copying "%1$s" to "%2$s"', $from, $to ) );
385
		}
386
	}
387
388
389
	/**
390
	 * Returns the flysystem adapter
391
	 *
392
	 * @return \League\Flysystem\AdapterInterface Flysystem adapter
393
	 */
394
	protected function getAdapter()
395
	{
396
		return $this->getProvider()->getAdapter();
397
	}
398
399
400
	/**
401
	 * Returns the configuration options
402
	 *
403
	 * @return array Configuration options
404
	 */
405
	protected function getConfig()
406
	{
407
		return $this->config;
408
	}
409
410
411
	/**
412
	 * Returns the file system provider
413
	 *
414
	 * @return \League\Flysystem\FilesystemInterface File system provider
415
	 */
416
	protected abstract function getProvider();
0 ignored issues
show
The abstract declaration must precede the visibility declaration
Loading history...
417
}
418