Passed
Push — master ( b62f3e...ec4003 )
by Aimeos
02:15
created

Standard::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package Base
7
 * @subpackage Filesystem
8
 */
9
10
11
namespace Aimeos\Base\Filesystem\Manager;
12
13
14
/**
15
 * Standard file system manager
16
 *
17
 * @package Base
18
 * @subpackage Filesystem
19
 */
20
class Standard implements Iface
21
{
22
	private $config;
23
	private $objects = [];
24
25
26
	/**
27
	 * Initializes the object
28
	 *
29
	 * @param array $config Configuration object
30
	 */
31
	public function __construct( array $config )
32
	{
33
		$this->config = $config;
34
	}
35
36
37
	/**
38
	 * Cleans up the object
39
	 */
40
	public function __destruct()
41
	{
42
		foreach( $this->objects as $key => $object ) {
43
			unset( $this->objects[$key] );
44
		}
45
	}
46
47
48
	/**
49
	 * Clean up the objects inside
50
	 */
51
	public function __sleep()
52
	{
53
		$this->__destruct();
54
		$this->objects = [];
55
56
		return get_object_vars( $this );
57
	}
58
59
60
	/**
61
	 * Returns the file system for the given name
62
	 *
63
	 * @param string $name Key for the file system
64
	 * @return \Aimeos\Base\Filesystem\Iface File system object
65
	 * @throws \Aimeos\Base\Filesystem\Exception If an no configuration for that name is found
66
	 */
67
	public function get( string $name ) : \Aimeos\Base\Filesystem\Iface
68
	{
69
		if( !isset( $this->objects[$name] ) ) {
70
			$this->objects[$name] = $this->create( $this->config( $name ) );
71
		}
72
73
		return $this->objects[$name];
74
	}
75
76
77
	/**
78
	 * Returns the configuration for the given name
79
	 *
80
	 * @param string $name Name of the resource, e.g. "fs" or "fs-media"
81
	 * @return array Configuration values
82
	 * @throws \Aimeos\Base\Filesystem\Exception If an no configuration for that name is found
83
	 */
84
	protected function config( string $name ) : array
85
	{
86
		foreach( [$name, 'fs'] as $fsname )
87
		{
88
			if( isset( $this->config[$fsname] ) ) {
89
				return $this->config[$fsname];
90
			}
91
		}
92
93
		$msg = sprintf( 'No resource configuration for "%1$s" available', $name );
94
		throw new \Aimeos\Base\Filesystem\Exception( $msg );
95
	}
96
97
98
	/**
99
	 * Creates and returns a new file system object
100
	 *
101
	 * @param array $config Resource configuration
102
	 * @return \Aimeos\Base\Filesystem\Iface File system object
103
	 * @throws \Aimeos\Base\Filesystem\Exception if file system class isn't found
104
	 */
105
	protected function create( array $config )
106
	{
107
		if( !isset( $config['adapter'] ) ) {
108
			throw new \Aimeos\Base\Filesystem\Exception( 'File system not configured' );
109
		}
110
111
		$classname = '\Aimeos\Base\Filesystem\\' . ucfirst( (string) $config['adapter'] );
112
113
		if( !class_exists( $classname ) ) {
114
			throw new \Aimeos\Base\Filesystem\Exception( sprintf( 'File system "%1$s" not found', $config['adapter'] ) );
115
		}
116
117
		return new $classname( $config );
118
	}
119
}
120