SetupCommand::setOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
		{--option= : Setup configuration, name and value are separated by ":" like "setup/default/demo:1"}
34
	';
35
36
	/**
37
	 * The console command description.
38
	 *
39
	 * @var string
40
	 */
41
	protected $description = 'Initialize or update the Aimeos database tables';
42
43
44
	/**
45
	 * Execute the console command.
46
	 *
47
	 * @return mixed
48
	 */
49
	public function handle()
50
	{
51
		$ctx = $this->getLaravel()->make( 'aimeos.context' )->get( false, 'command' );
52
		$ctx->setEditor( 'aimeos:setup' );
53
54
		$config = $ctx->getConfig();
55
		$site = $this->argument( 'site' );
56
		$template = $this->argument( 'tplsite' );
57
58
		$config->set( 'setup/site', $site );
59
		$dbconfig = $this->getDbConfig( $config );
60
		$this->setOptions( $config );
61
62
		\Aimeos\MShop::cache( false );
63
		\Aimeos\MAdmin::cache( false );
64
65
		$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

65
		$taskPaths = $this->getLaravel()->make( 'aimeos' )->get()->getSetupPaths( /** @scrutinizer ignore-type */ $template );
Loading history...
66
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
67
68
		$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

68
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', /** @scrutinizer ignore-type */ $site ) );
Loading history...
69
70
		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...
71
			$task = reset( $task );
72
		}
73
74
		$manager->migrate( $task );
75
	}
76
77
78
	/**
79
	 * Returns the database configuration from the config object.
80
	 *
81
	 * @param \Aimeos\MW\Config\Iface $conf Config object
82
	 * @return array Multi-dimensional associative list of database configuration parameters
83
	 */
84
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf ) : array
85
	{
86
		$dbconfig = $conf->get( 'resource', array() );
87
88
		foreach( $dbconfig as $rname => $dbconf )
89
		{
90
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
91
				unset( $dbconfig[$rname] );
92
			} else {
93
				$conf->set( 'resource/' . $rname . '/limit', 5 );
94
			}
95
		}
96
97
		return $dbconfig;
98
	}
99
100
101
	/**
102
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
103
	 *
104
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
105
	 * @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...
106
	 * @throws \RuntimeException If the format of the options is invalid
107
	 */
108
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
109
	{
110
		foreach( (array) $this->option( 'option' ) as $option )
111
		{
112
			list( $name, $value ) = explode( ':', $option );
113
			$conf->set( str_replace( '\\', '/', $name ), $value );
114
		}
115
	}
116
}
117