Passed
Push — master ( 45eff3...c546eb )
by Aimeos
06:00
created

Setup::macros()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 34
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 56
rs 8.7537

How to fix   Long Method   

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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 */
7
8
9
namespace Aimeos;
10
11
12
/**
13
 * Setup Aimeos data structures and storeages
14
 */
15
class Setup
16
{
17
	private $bootstrap;
18
	private $config;
19
	private $context;
20
	private $verbose;
21
22
23
	/**
24
	 * Initializes the Aimeos setup object
25
	 *
26
	 * @param Bootstrap $bootstrap Aimeos bootstrap object
27
	 * @param array $options Associative list of config keys and values
28
	 */
29
	public function __construct( Bootstrap $bootstrap, array $options = [] )
30
	{
31
		$this->config = $this->createConfig( $bootstrap->getConfigPaths(), $options );
32
		$this->bootstrap = $bootstrap;
33
34
		$this->macros();
35
	}
36
37
38
	/**
39
	 * Creates a new initialized setup object
40
	 *
41
	 * @param Bootstrap $bootstrap Aimeos bootstrap object
42
	 * @param array $options Associative list of config keys and values
43
	 * @return self Aimeos setup object
44
	 */
45
	public static function use( Bootstrap $bootstrap, array $options = [] ) : self
46
	{
47
		return new static( $bootstrap, $options );
48
	}
49
50
51
	/**
52
	 * Sets a custom context object
53
	 *
54
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
55
	 * @return self Same object for fluid method calls
56
	 */
57
	public function context( \Aimeos\MShop\Context\Item\Iface $context ) : self
58
	{
59
		$this->context = $context;
60
		return $this;
61
	}
62
63
64
	/**
65
	 * Performs the migrations
66
	 *
67
	 * @param string $site Site code to execute the migrations for
68
	 * @param string $template Name of the migration template
69
	 */
70
	public function up( string $site = 'default', string $template = 'default' )
71
	{
72
		$ctx = $this->context ?? $this->createContext( $this->config );
73
74
		\Aimeos\Upscheme\Task\Base::macro( 'context', function() use ( $ctx ) {
75
			return $ctx;
76
		} );
77
78
79
		$this->config->set( 'setup/site', $site );
80
		$dbconf = $this->getDBConfig( $this->config );
81
		$taskPaths = $this->bootstrap->getSetupPaths( $template );
82
83
		\Aimeos\Upscheme\Up::use( $dbconf, $taskPaths )->verbose( $this->verbose )->up();
84
	}
85
86
87
	/**
88
	 * Sets the verbosity level
89
	 *
90
	 * @param mixed $level Verbosity level (empty: none, v: notice: vv: info, vvv: debug)
91
	 * @return self Same object for fluid method calls
92
	 */
93
	public function verbose( $level = 'vv' ) : self
94
	{
95
		$this->verbose = $level;
96
		return $this;
97
	}
98
99
100
	/**
101
	 * Returns a new configuration object
102
	 *
103
	 * @param array $confPaths List of configuration paths from the bootstrap object
104
	 * @param array $options Associative list of configuration options as key/value pairs
105
	 * @return \Aimeos\MW\Config\Iface Configuration object
106
	 */
107
	protected function createConfig( array $confPaths, array $options ) : \Aimeos\MW\Config\Iface
108
	{
109
		$config = [];
110
111
		foreach( (array) ( $options['config'] ?? [] ) as $path )
112
		{
113
			if( is_file( $path ) ) {
114
				$config = array_replace_recursive( $config, require $path );
115
			} else {
116
				$confPaths[] = $path;
117
			}
118
		}
119
120
		$conf = new \Aimeos\MW\Config\PHPArray( $config, $confPaths );
121
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
122
123
		foreach( (array) ( $options['option'] ?? [] ) as $option )
124
		{
125
			$parts = explode( ':', $option );
126
127
			if( count( $parts ) !== 2 ) {
128
				throw new \RuntimeException( "Invalid config option \"%1\$s\"\n", $option );
129
			}
130
131
			$conf->set( str_replace( '\\', '/', $parts[0] ), $parts[1] );
132
		}
133
134
		return $conf;
135
	}
136
137
138
	/**
139
	 * Returns a new context object
140
	 *
141
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
142
	 * @return \Aimeos\MShop\Context\Item\Iface New context object
143
	 */
144
	protected function createContext( \Aimeos\MW\Config\Iface $conf ) : \Aimeos\MShop\Context\Item\Iface
145
	{
146
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
147
		$ctx->setConfig( $conf );
148
149
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $conf );
150
		$ctx->setDatabaseManager( $dbm );
151
152
		$logger = new \Aimeos\MW\Logger\Errorlog( \Aimeos\MW\Logger\Base::INFO );
153
		$ctx->setLogger( $logger );
154
155
		$password = new \Aimeos\MW\Password\Standard();
156
		$ctx->setPassword( $password );
157
158
		$session = new \Aimeos\MW\Session\None();
159
		$ctx->setSession( $session );
160
161
		$cache = new \Aimeos\MW\Cache\None();
162
		$ctx->setCache( $cache );
163
164
		$process = new \Aimeos\MW\Process\Pcntl( $conf->get( 'pcntl_max', 4 ), $conf->get( 'pcntl_priority', 19 ) );
165
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
166
		$ctx->setProcess( $process );
167
168
		return $ctx;
169
	}
170
171
172
	/**
173
	 * Returns the database configuration
174
	 *
175
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
176
	 * @return array Database configuration
177
	 */
178
	protected function getDBConfig( \Aimeos\MW\Config\Iface $conf ) : array
179
	{
180
		$dbconfig = $conf->get( 'resource', [] );
181
182
		foreach( $dbconfig as $rname => $dbconf )
183
		{
184
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
185
				unset( $dbconfig[$rname] );
186
			} else {
187
				$conf->set( 'resource/' . $rname . '/limit', 5 );
188
			}
189
		}
190
191
		return $dbconfig;
192
	}
193
194
195
	/**
196
	 * Adds the required marcos to the Upscheme objects
197
	 */
198
	protected function macros()
199
	{
200
		\Aimeos\Upscheme\Up::macro( 'connect', function( array $cfg ) {
201
202
			switch( $cfg['adapter'] )
203
			{
204
				case 'mysql': $cfg['driver'] = 'pdo_mysql'; break;
205
				case 'pgsql': $cfg['driver'] = 'pdo_pgsql'; break;
206
				case 'sqlsrv': $cfg['driver'] = 'pdo_sqlsrv'; break;
207
				default: $cfg['driver'] = $cfg['adapter'];
208
			}
209
210
			if( isset( $cfg['database'] ) ) {
211
				$cfg['dbname'] = $cfg['database'];
212
			}
213
214
			if( isset( $cfg['username'] ) ) {
215
				$cfg['user'] = $cfg['username'];
216
			}
217
218
			unset( $cfg['adapter'], $cfg['database'], $cfg['username'] );
219
220
			return \Doctrine\DBAL\DriverManager::getConnection( $cfg );
221
		} );
222
223
224
		\Aimeos\Upscheme\Schema\Table::macro( 'startend', function() {
225
			$this->datetime( 'start' )->null( true );
0 ignored issues
show
Bug introduced by
The method datetime() does not exist on Aimeos\Setup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
			$this->/** @scrutinizer ignore-call */ 
226
          datetime( 'start' )->null( true );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
226
			return $this->datetime( 'end' )->null( true );
227
		} );
228
229
		\Aimeos\Upscheme\Schema\Table::macro( 'code', function( string $name = 'code' ) {
230
			return $this->string( $name, 64 )
0 ignored issues
show
Bug introduced by
The method string() does not exist on Aimeos\Setup. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

230
			return $this->/** @scrutinizer ignore-call */ string( $name, 64 )

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
231
				->opt( 'charset', 'utf8', 'mysql' )
232
				->opt( 'collation', 'utf8_bin', 'mysql' )
233
				->default( '' );
234
		} );
235
236
		\Aimeos\Upscheme\Schema\Table::macro( 'type', function( string $name = 'type' ) {
237
			return $this->string( $name, 64 )
238
				->opt( 'charset', 'utf8', 'mysql' )
239
				->opt( 'collation', 'utf8_bin', 'mysql' )
240
				->default( '' );
241
		} );
242
243
		\Aimeos\Upscheme\Schema\Table::macro( 'refid', function( string $name = 'refid' ) {
244
			return $this->string( $name, 36 )
245
				->opt( 'charset', 'utf8', 'mysql' )
246
				->opt( 'collation', 'utf8_bin', 'mysql' )
247
				->default( '' );
248
		} );
249
250
		\Aimeos\Upscheme\Schema\Table::macro( 'meta', function() {
251
			$this->datetime( 'mtime' );
252
			$this->datetime( 'ctime' );
253
			return $this->string( 'editor' );
254
		} );
255
	}
256
}
257