Completed
Push — master ( f165f0...a36629 )
by Aimeos
09:56 queued 02:40
created

Laravel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015
6
 * @package MW
7
 * @subpackage Filesystem
8
 */
9
10
11
namespace Aimeos\MW\Filesystem;
12
13
14
/**
15
 * Implementation of basic file system methods
16
 *
17
 * @package MW
18
 * @subpackage Filesystem
19
 */
20
class Laravel implements BasicIface, DirIface, MetaIface
21
{
22
	private $fs;
23
24
25
	/**
26
	 * Initializes the object
27
	 *
28
	 * @param string $basepath Root path to the file system
0 ignored issues
show
Bug introduced by
There is no parameter named $basepath. 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...
29
	 */
30
	public function __construct( \Illuminate\Contracts\Filesystem\Filesystem $fs )
31
	{
32
		$this->fs = $fs;
33
	}
34
35
36
	/**
37
	 * Tests if the given path is a directory
38
	 *
39
	 * @param string $path Path to the file or directory
40
	 * @return boolean True if directory, false if not
41
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
42
	 */
43
	public function isdir( $path )
44
	{
45
		return in_array( basename( $path ), $this->fs->directories( dirname( $path ) ) );
46
	}
47
48
49
	/**
50
	 * Creates a new directory for the given path
51
	 *
52
	 * @param string $path Path to the directory
53
	 * @return void
54
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
55
	*/
56
	public function mkdir( $path )
57
	{
58
		try {
59
			$this->fs->makeDirectory( $path );
60
		} catch( \Exception $e ) {
61
			throw new Exception( $e->getMessage(), 0, $e );
62
		}
63
	}
64
65
66
	/**
67
	 * Deletes the directory for the given path
68
	 *
69
	 * @param string $path Path to the directory
70
	 * @return void
71
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
72
	*/
73
	public function rmdir( $path )
74
	{
75
		try {
76
			$this->fs->deleteDirectory( $path );
77
		} catch( \Exception $e ) {
78
			throw new Exception( $e->getMessage(), 0, $e );
79
		}
80
	}
81
82
83
	/**
84
	 * Returns an iterator over the entries in the given path
85
	 *
86
	 * {@inheritDoc}
87
	 *
88
	 * @param string $path Path to the filesystem or directory
89
	 * @return \Iterator|array Iterator over the entries or array with entries
90
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
91
	 */
92
	public function scan( $path = null )
93
	{
94
		try {
95
			return array_merge( $this->fs->directories( $path ), $this->fs->files( $path ) );
96
		} catch( \Exception $e ) {
97
			throw new Exception( $e->getMessage(), 0, $e );
98
		}
99
	}
100
101
102
	/**
103
	 * Returns the file size
104
	 *
105
	 * @param string $path Path to the file
106
	 * @return integer Size in bytes
107
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
108
	 */
109
	public function size( $path )
110
	{
111
		try {
112
			return $this->fs->size( $path );
113
		} catch( \Exception $e ) {
114
			throw new Exception( $e->getMessage(), 0, $e );
115
		}
116
	}
117
118
119
	/**
120
	 * Returns the Unix time stamp for the file
121
	 *
122
	 * @param string $path Path to the file
123
	 * @return integer Unix time stamp in seconds
124
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
125
	 */
126
	public function time( $path )
127
	{
128
		try {
129
			return $this->fs->lastModified( $path );
130
		} catch( \Exception $e ) {
131
			throw new Exception( $e->getMessage(), 0, $e );
132
		}
133
	}
134
135
136
	/**
137
	 * Deletes the file for the given path
138
	 *
139
	 * @param string $path Path to the file
140
	 * @return void
141
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
142
	 */
143
	public function rm( $path )
144
	{
145
		try {
146
			$this->fs->delete( $path );
147
		} catch( \Exception $e ) {
148
			throw new Exception( $e->getMessage(), 0, $e );
149
		}
150
	}
151
152
153
	/**
154
	 * Tests if a file exists at the given path
155
	 *
156
	 * @param string $path Path to the file
157
	 * @return boolean True if it exists, false if not
158
	 */
159
	public function has( $path )
160
	{
161
		return $this->fs->exists( $path );
162
	}
163
164
165
	/**
166
	 * Returns the content of the file
167
	 *
168
	 * {@inheritDoc}
169
	 *
170
	 * @param string $path Path to the file
171
	 * @return string File content
172
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
173
	 */
174
	public function read( $path )
175
	{
176
		try {
177
			return $this->fs->get( $path );
178
		} catch( \Exception $e ) {
179
			throw new Exception( $e->getMessage(), 0, $e );
180
		}
181
	}
182
183
184
	/**
185
	 * Returns the stream descriptor for the file
186
	 *
187
	 * {@inheritDoc}
188
	 *
189
	 * @param string $path Path to the file
190
	 * @return resource File stream descriptor
191
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
192
	 */
193
	public function reads( $path )
194
	{
195
		try {
196
			$content = $this->fs->get( $path );
197
		} catch( \Exception $e ) {
198
			throw new Exception( $e->getMessage(), 0, $e );
199
		}
200
201
		if( ( $stream = tmpfile() ) === false ) {
202
			$error = error_get_last();
203
			throw new Exception( $error['message'] );
204
		}
205
206 View Code Duplication
		if( fwrite( $stream, $content ) === false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
207
			$error = error_get_last();
208
			throw new Exception( $error['message'] );
209
		}
210
211 View Code Duplication
		if( rewind( $stream ) === false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
212
			$error = error_get_last();
213
			throw new Exception( $error['message'] );
214
		}
215
216
		return $stream;
217
	}
218
219
220
	/**
221
	 * Writes the given content to the file
222
	 *
223
	 * {@inheritDoc}
224
	 *
225
	 * @param string $path Path to the file
226
	 * @param string $content New file content
227
	 * @return void
228
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
229
	 */
230
	public function write( $path, $content )
231
	{
232
		try {
233
			$this->fs->put( $path, $content );
234
		} catch( \Exception $e ) {
235
			throw new Exception( $e->getMessage(), 0, $e );
236
		}
237
	}
238
239
240
	/**
241
	 * Write the content of the stream descriptor into the remote file
242
	 *
243
	 * {@inheritDoc}
244
	 *
245
	 * @param string $path Path to the file
246
	 * @param resource $stream File stream descriptor
247
	 * @return void
248
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
249
	 */
250
	public function writes( $path, $stream )
251
	{
252 View Code Duplication
		if( ( $content = @fread( $stream, 0x7fffffff ) ) === false ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
253
			$error = error_get_last();
254
			throw new Exception( $error['message'] );
255
		}
256
257
		try {
258
			$content = $this->fs->put( $path, $content );
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
259
		} catch( \Exception $e ) {
260
			throw new Exception( $e->getMessage(), 0, $e );
261
		}
262
	}
263
264
265
	/**
266
	 * Renames a file, moves it to a new location or both at once
267
	 *
268
	 * @param string $from Path to the original file
269
	 * @param string $to Path to the new file
270
	 * @return void
271
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
272
	 */
273
	public function move( $from, $to )
274
	{
275
		try {
276
			$this->fs->move( $from, $to );
277
		} catch( \Exception $e ) {
278
			throw new Exception( $e->getMessage(), 0, $e );
279
		}
280
	}
281
282
283
	/**
284
	 * Copies a file to a new location
285
	 *
286
	 * @param string $from Path to the original file
287
	 * @param string $to Path to the new file
288
	 * @return void
289
	 * @throws \Aimeos\MW\Filesystem\Exception If an error occurs
290
	 */
291
	public function copy( $from, $to )
292
	{
293
		try {
294
			$this->fs->copy( $from, $to );
295
		} catch( \Exception $e ) {
296
			throw new Exception( $e->getMessage(), 0, $e );
297
		}
298
	}
299
}
300