Completed
Push — master ( 9b0aba...0a2150 )
by Aimeos
15:35
created

Factory::get()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
cc 6
eloc 14
nc 4
nop 2
rs 8.439
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MW
8
 * @subpackage Media
9
 */
10
11
12
namespace Aimeos\MW\Media;
13
14
15
/**
16
 * Creates a new media object.
17
 *
18
 * @package MW
19
 * @subpackage Media
20
 */
21
class Factory
22
{
23
	/**
24
	 * Creates a new media object.
25
	 *
26
	 * Options for the factory are:
27
	 * - image: Associative list of image related options
28
	 * - application: Associative list of application related options
29
	 *
30
	 * @param resource|string $file File resource, path to the file or file content
31
	 * @param array $options Associative list of options for configuring the media class
32
	 * @return \Aimeos\MW\Media\Iface Media object
33
	 */
34
	public static function get( $file, array $options = array() )
35
	{
36
		$content = $file;
37
		$mimetype = 'application/octet-stream';
0 ignored issues
show
Unused Code introduced by
$mimetype 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...
38
39
		if( @is_resource( $file ) && ( $content = stream_get_contents( $file ) ) === false ) {
40
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from stream' ) );
41
		}
42
43
		if( @is_file( $file ) && ( $content = @file_get_contents( $file ) ) === false ) {
44
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $file ) );
45
		}
46
47
		$finfo = new \finfo( FILEINFO_MIME_TYPE );
48
		$mimetype = $finfo->buffer( $content );
49
		$mimeparts = explode( '/', $mimetype );
50
51
		switch( $mimeparts[0] )
52
		{
53
			case 'image':
54
				return new \Aimeos\MW\Media\Image\Standard( $content, $mimetype, $options );
55
		}
56
57
		return new \Aimeos\MW\Media\Application\Standard( $content, $mimetype, $options );
58
	}
59
}
60