Passed
Push — master ( 16ddec...5bff87 )
by Aimeos
05:22
created

Setup::createConfig()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
nc 9
nop 2
dl 0
loc 28
c 1
b 0
f 0
cc 5
rs 9.4888
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 $context;
19
	private $config;
20
	private $verbose;
21
22
23
	/**
24
	 * Initializes the Aimeos setup object
25
	 *
26
	 * @param Bootstrap $bootstrap Aimeos bootstrap object
27
	 * @param array $config Associative list of config keys and values
28
	 */
29
	public function __construct( Bootstrap $bootstrap, array $config = [] )
30
	{
31
		$this->bootstrap = $bootstrap;
32
		$this->config = $config;
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 $config Associative list of config keys and values
43
	 * @return self Aimeos setup object
44
	 */
45
	public static function use( Bootstrap $bootstrap, array $config = [] ) : self
46
	{
47
		return new static( $bootstrap, $config );
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() )->setEditor( 'setup' );
73
74
		\Aimeos\Upscheme\Task\Base::macro( 'context', function() use ( $ctx ) {
75
			return $ctx;
76
		} );
77
78
79
		$config = $ctx->config();
80
		$config->set( 'setup/site', $site );
81
		$dbconf = $this->getDBConfig( $config );
82
		$taskPaths = $this->bootstrap->getSetupPaths( $template );
83
84
		\Aimeos\Upscheme\Up::use( $dbconf, $taskPaths )->verbose( $this->verbose )->up();
85
	}
86
87
88
	/**
89
	 * Sets the verbosity level
90
	 *
91
	 * @param mixed $level Verbosity level (empty: none, v: notice: vv: info, vvv: debug)
92
	 * @return self Same object for fluid method calls
93
	 */
94
	public function verbose( $level = 'vv' ) : self
95
	{
96
		$this->verbose = $level;
97
		return $this;
98
	}
99
100
101
	/**
102
	 * Returns a new context object
103
	 *
104
	 * @return \Aimeos\MShop\Context\Item\Iface New context object
105
	 */
106
	protected function createContext() : \Aimeos\MShop\Context\Item\Iface
107
	{
108
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
109
110
		$conf = new \Aimeos\MW\Config\PHPArray( $this->config, $this->bootstrap->getConfigPaths() );
111
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
112
		$ctx->setConfig( $conf );
113
114
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $conf );
115
		$ctx->setDatabaseManager( $dbm );
116
117
		$logger = new \Aimeos\MW\Logger\Errorlog( \Aimeos\MW\Logger\Base::INFO );
118
		$ctx->setLogger( $logger );
119
120
		$password = new \Aimeos\MW\Password\Standard();
121
		$ctx->setPassword( $password );
122
123
		$session = new \Aimeos\MW\Session\None();
124
		$ctx->setSession( $session );
125
126
		$cache = new \Aimeos\MW\Cache\None();
127
		$ctx->setCache( $cache );
128
129
		$process = new \Aimeos\MW\Process\Pcntl( $conf->get( 'pcntl_max', 4 ), $conf->get( 'pcntl_priority', 19 ) );
130
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
131
		$ctx->setProcess( $process );
132
133
		return $ctx;
134
	}
135
136
137
	/**
138
	 * Returns the database configuration
139
	 *
140
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
141
	 * @return array Database configuration
142
	 */
143
	protected function getDBConfig( \Aimeos\MW\Config\Iface $conf ) : array
144
	{
145
		$dbconfig = $conf->get( 'resource', [] );
146
147
		foreach( $dbconfig as $rname => $entry )
148
		{
149
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
150
				unset( $dbconfig[$rname] );
151
			}
152
		}
153
154
		return $dbconfig;
155
	}
156
157
158
	/**
159
	 * Adds the required marcos to the Upscheme objects
160
	 */
161
	protected function macros()
162
	{
163
		\Aimeos\Upscheme\Up::macro( 'connect', function( array $cfg ) {
164
165
			switch( $cfg['adapter'] )
166
			{
167
				case 'mysql': $cfg['driver'] = 'pdo_mysql'; break;
168
				case 'oracle': $cfg['driver'] = 'pdo_oci'; break;
169
				case 'pgsql': $cfg['driver'] = 'pdo_pgsql'; break;
170
				case 'sqlsrv': $cfg['driver'] = 'pdo_sqlsrv'; break;
171
				default: $cfg['driver'] = $cfg['adapter'];
172
			}
173
174
			if( isset( $cfg['database'] ) ) {
175
				$cfg['dbname'] = $cfg['database'];
176
			}
177
178
			if( isset( $cfg['username'] ) ) {
179
				$cfg['user'] = $cfg['username'];
180
			}
181
182
			unset( $cfg['adapter'], $cfg['database'], $cfg['username'] );
183
184
			return \Doctrine\DBAL\DriverManager::getConnection( $cfg );
185
		} );
186
187
188
		\Aimeos\Upscheme\Schema\Table::macro( 'startend', function() {
189
			$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

189
			$this->/** @scrutinizer ignore-call */ 
190
          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...
190
			return $this->datetime( 'end' )->null( true );
191
		} );
192
193
		\Aimeos\Upscheme\Schema\Table::macro( 'code', function( string $name = 'code' ) {
194
			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

194
			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...
195
				->opt( 'charset', 'utf8', 'mysql' )
196
				->opt( 'collation', 'utf8_bin', 'mysql' )
197
				->default( '' );
198
		} );
199
200
		\Aimeos\Upscheme\Schema\Table::macro( 'type', function( string $name = 'type' ) {
201
			return $this->string( $name, 64 )
202
				->opt( 'charset', 'utf8', 'mysql' )
203
				->opt( 'collation', 'utf8_bin', 'mysql' )
204
				->default( '' );
205
		} );
206
207
		\Aimeos\Upscheme\Schema\Table::macro( 'refid', function( string $name = 'refid' ) {
208
			return $this->string( $name, 36 )
209
				->opt( 'charset', 'utf8', 'mysql' )
210
				->opt( 'collation', 'utf8_bin', 'mysql' )
211
				->default( '' );
212
		} );
213
214
		\Aimeos\Upscheme\Schema\Table::macro( 'meta', function() {
215
			$this->datetime( 'mtime' );
216
			$this->datetime( 'ctime' );
217
			return $this->string( 'editor' );
218
		} );
219
	}
220
}
221