Passed
Push — master ( 449e92...8f78ae )
by Aimeos
06:15
created

Factory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 78
rs 10
c 2
b 0
f 0
wmc 18

1 Method

Rating   Name   Duplication   Size   Complexity  
F get() 0 65 18
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2020
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 = [] )
35
	{
36
		if( is_resource( $file ) )
37
		{
38
			if( ( $content = stream_get_contents( $file ) ) === false ) {
39
				throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from stream' ) );
40
			}
41
		}
42
		elseif( is_string( $file ) )
43
		{
44
			if( strpos( $file, "\0" ) === false && is_file( $file ) )
45
			{
46
				if( ( $content = file_get_contents( $file ) ) === false ) {
47
					throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $file ) );
48
				}
49
			}
50
			else
51
			{
52
				$content = $file;
53
			}
54
		}
55
		else
56
		{
57
			throw new \Aimeos\MW\Media\Exception( 'Unsupported file parameter type' );
58
		}
59
60
61
		$finfo = new \finfo( FILEINFO_MIME_TYPE );
62
		$mimetype = $finfo->buffer( $content );
63
		$mime = explode( '/', $mimetype );
64
65
		$type = $mime[0] === 'image' ? 'Image' : 'Application';
66
		$name = $type === 'Image' && extension_loaded( 'imagick' ) ? 'Imagick' : 'Standard';
67
		$name = ucfirst( $options[$mime[0]]['name'] ?? $name );
68
69
		if( in_array( $mimetype, ['image/svg', 'image/svg+xml'] )
70
			|| in_array( $mimetype, ['application/gzip', 'application/x-gzip'] )
71
			&& is_string( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), ['svg', 'svgz'] )
72
		) {
73
			$mimetype = 'image/svg+xml';
74
			$type = 'Image';
75
			$name = 'Svg';
76
		}
77
78
79
		if( ctype_alnum( $name ) === false )
80
		{
81
			$classname = is_string( $name ) ? '\Aimeos\MW\Media\\' . $type . '\\' . $name : '<not a string>';
82
			throw new \Aimeos\MW\Container\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
83
		}
84
85
		$iface = \Aimeos\MW\Media\Iface::class;
86
		$classname = '\Aimeos\MW\Media\\' . $type . '\\' . $name;
87
88
		if( class_exists( $classname ) === false ) {
89
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
90
		}
91
92
		$object = new $classname( $content, $mimetype, $options );
93
94
		if( !( $object instanceof $iface ) ) {
95
			throw new \Aimeos\MW\Media\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) );
96
		}
97
98
		return $object;
99
	}
100
}
101