SetupCommand::handle()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 38
rs 8.8657
cc 6
nc 8
nop 0
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\Shop\Command;
12
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Input\InputArgument;
15
16
17
/**
18
 * Command for initializing or updating the Aimeos database tables
19
 * @package laravel
20
 * @subpackage Command
21
 */
22
class SetupCommand extends AbstractCommand
23
{
24
	/**
25
	 * The name and signature of the console command.
26
	 *
27
	 * @var string
28
	 */
29
	protected $signature = 'aimeos:setup
30
		{site=default : Site for updating database entries}
31
		{tplsite=default : Site used as template for creating the new one}
32
		{--task : Name of the setup task that should be executed}
33
		{--action=migrate : Action name that should be executed, i.e. "migrate", "rollback", "clean"}
34
		{--option= : Setup configuration, name and value are separated by ":" like "setup/default/demo:1"}
35
	';
36
37
	/**
38
	 * The console command description.
39
	 *
40
	 * @var string
41
	 */
42
	protected $description = 'Initialize or update the Aimeos database tables';
43
44
45
	/**
46
	 * Execute the console command.
47
	 *
48
	 * @return mixed
49
	 */
50
	public function handle()
51
	{
52
		$ctx = $this->getLaravel()->make( 'aimeos.context' )->get( false, 'command' );
53
		$ctx->setEditor( 'aimeos:setup' );
54
55
		$config = $ctx->getConfig();
56
		$site = $this->argument( 'site' );
57
		$template = $this->argument( 'tplsite' );
58
59
		$config->set( 'setup/site', $site );
60
		$dbconfig = $this->getDbConfig( $config );
61
		$this->setOptions( $config );
62
63
		\Aimeos\MShop::cache( false );
64
		\Aimeos\MAdmin::cache( false );
65
66
		$taskPaths = $this->getLaravel()->make( 'aimeos' )->get()->getSetupPaths( $template );
0 ignored issues
show
Bug introduced by Andreas
It seems like $template can also be of type string[]; however, parameter $site of Aimeos\Bootstrap::getSetupPaths() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
		$taskPaths = $this->getLaravel()->make( 'aimeos' )->get()->getSetupPaths( /** @scrutinizer ignore-type */ $template );
Loading history...
67
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
68
69
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) );
0 ignored issues
show
Bug introduced by Andreas
It seems like $site can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

69
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', /** @scrutinizer ignore-type */ $site ) );
Loading history...
70
71
		if( ( $task = $this->option( 'task' ) ) && is_array( $task ) ) {
0 ignored issues
show
introduced by Andreas
The condition is_array($task) is always false.
Loading history...
72
			$task = reset( $task );
73
		}
74
75
		switch( $this->option( 'action' ) )
76
		{
77
			case 'migrate':
78
				$manager->migrate( $task );
79
				break;
80
			case 'rollback':
81
				$manager->rollback( $task );
82
				break;
83
			case 'clean':
84
				$manager->clean( $task );
85
				break;
86
			default:
87
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $this->option( 'action' ) ) );
88
		}
89
	}
90
91
92
	/**
93
	 * Returns the database configuration from the config object.
94
	 *
95
	 * @param \Aimeos\MW\Config\Iface $conf Config object
96
	 * @return array Multi-dimensional associative list of database configuration parameters
97
	 */
98
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
99
	{
100
		$dbconfig = $conf->get( 'resource', array() );
101
102
		foreach( $dbconfig as $rname => $dbconf )
103
		{
104
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
105
				unset( $dbconfig[$rname] );
106
			}
107
		}
108
109
		return $dbconfig;
110
	}
111
112
113
	/**
114
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
115
	 *
116
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
117
	 * @param array Associative list of database configurations
0 ignored issues
show
Bug introduced by Andreas
The type Aimeos\Shop\Command\Associative was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
118
	 * @throws \RuntimeException If the format of the options is invalid
119
	 */
120
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
121
	{
122
		foreach( (array) $this->option( 'option' ) as $option )
123
		{
124
			list( $name, $value ) = explode( ':', $option );
125
			$conf->set( str_replace( '\\', '/', $name ), $value );
126
		}
127
	}
128
}
129