Completed
Push — master ( e595c8...dedff5 )
by Aimeos
08:25
created

Factory::get()   C

Complexity

Conditions 11
Paths 22

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 22
nop 2
dl 0
loc 42
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
38
		if( @is_resource( $file ) && ( $content = stream_get_contents( $file ) ) === false ) {
39
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from stream' ) );
40
		}
41
42
		if( @is_file( $file ) && ( $content = @file_get_contents( $file ) ) === false ) {
43
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $file ) );
44
		}
45
46
47
		$finfo = new \finfo( FILEINFO_MIME_TYPE );
48
		$mimetype = $finfo->buffer( $content );
49
		$mime = explode( '/', $mimetype );
50
51
		$type = ( $mime[0] === 'image' ? 'Image' : 'Application' );
52
		$name = ( isset( $options[ $mime[0] ]['name'] ) ? ucfirst( $options[ $mime[0] ]['name'] ) : 'Standard' );
53
54
55
		if( ctype_alnum( $name ) === false )
56
		{
57
			$classname = is_string( $name ) ? '\\Aimeos\\MW\\Media\\' . $name : '<not a string>';
58
			throw new \Aimeos\MW\Container\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
59
		}
60
61
		$iface = '\\Aimeos\\MW\\Media\\Iface';
62
		$classname = '\\Aimeos\\MW\\Media\\' . $type . '\\' . $name;
63
64
		if( class_exists( $classname ) === false ) {
65
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
66
		}
67
68
		$object = new $classname( $content, $mimetype, $options );
69
70
		if( !( $object instanceof $iface ) ) {
71
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) );
72
		}
73
74
		return $object;
75
	}
76
}
77