Passed
Push — master ( 48d2af...a4afe2 )
by Aimeos
11:39
created

SetupCommand::setOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package symfony
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\ShopBundle\Command;
12
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
19
/**
20
 * Performs the database initialization and update.
21
 *
22
 * @package symfony
23
 * @subpackage Command
24
 */
25
class SetupCommand extends Command
26
{
27
	protected static $defaultName = 'aimeos:setup';
28
29
30
	/**
31
	 * Configures the command name and description.
32
	 */
33
	protected function configure()
34
	{
35
		$this->setName( self::$defaultName );
36
		$this->setDescription( 'Initialize or update the Aimeos database tables' );
37
		$this->addArgument( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' );
38
		$this->addArgument( 'tplsite', InputArgument::OPTIONAL, 'Template site for creating or updating database entries', 'default' );
39
		$this->addOption( 'option', null, InputOption::VALUE_REQUIRED, 'Optional setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() );
40
		$this->addOption( 'action', null, InputOption::VALUE_REQUIRED, 'Action name that should be executed, i.e. "migrate", "rollback", "clean"', 'migrate' );
41
		$this->addOption( 'task', null, InputOption::VALUE_REQUIRED, 'Name of the setup task that should be executed', null );
42
	}
43
44
45
	/**
46
	 * Executes the database initialization and update.
47
	 *
48
	 * @param InputInterface $input Input object
49
	 * @param OutputInterface $output Output object
50
	 */
51
	protected function execute( InputInterface $input, OutputInterface $output )
52
	{
53
		$ctx = $this->getContainer()->get( 'aimeos.context' )->get( false, 'command' );
54
		$ctx->setEditor( 'aimeos:setup' );
55
56
		$config = $ctx->getConfig();
57
		$site = $input->getArgument( 'site' );
58
		$tplsite = $input->getArgument( 'tplsite' );
59
60
		$config->set( 'setup/site', $site );
61
		$dbconfig = $this->getDbConfig( $config );
62
		$this->setOptions( $config, $input );
63
64
		\Aimeos\MShop::cache( false );
65
		\Aimeos\MAdmin::cache( false );
66
67
		$taskPaths = $this->getContainer()->get( 'aimeos' )->get()->getSetupPaths( $tplsite );
68
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
69
70
		$output->writeln( sprintf( 'Initializing or updating the Aimeos database tables for site <info>%1$s</info>', $site ) );
0 ignored issues
show
Bug introduced by
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

70
		$output->writeln( sprintf( 'Initializing or updating the Aimeos database tables for site <info>%1$s</info>', /** @scrutinizer ignore-type */ $site ) );
Loading history...
71
72
		if( ( $task = $input->getOption( 'task' ) ) && is_array( $task ) ) {
73
			$task = reset( $task );
74
		}
75
76
		switch( $input->getOption( 'action' ) )
77
		{
78
			case 'migrate':
79
				$manager->migrate( $task );
80
				break;
81
			case 'rollback':
82
				$manager->rollback( $task );
0 ignored issues
show
Bug introduced by
The method rollback() does not exist on Aimeos\MW\Setup\Manager\Multiple. ( Ignorable by Annotation )

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

82
				$manager->/** @scrutinizer ignore-call */ 
83
              rollback( $task );

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...
83
				break;
84
			case 'clean':
85
				$manager->clean( $task );
0 ignored issues
show
Bug introduced by
The method clean() does not exist on Aimeos\MW\Setup\Manager\Multiple. ( Ignorable by Annotation )

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

85
				$manager->/** @scrutinizer ignore-call */ 
86
              clean( $task );

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...
86
				break;
87
			default:
88
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $input->getOption( 'action' ) ) );
89
		}
90
	}
91
92
93
	/**
94
	 * Returns the database configuration from the config object.
95
	 *
96
	 * @param \Aimeos\MW\Config\Iface $conf Config object
97
	 * @return array Multi-dimensional associative list of database configuration parameters
98
	 */
99
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf ) : array
100
	{
101
		$dbconfig = $conf->get( 'resource', array() );
102
103
		foreach( $dbconfig as $rname => $dbconf )
104
		{
105
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
106
				unset( $dbconfig[$rname] );
107
			} else {
108
				$conf->set( 'resource/' . $rname . '/limit', 5 );
109
			}
110
		}
111
112
		return $dbconfig;
113
	}
114
115
116
	/**
117
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
118
	 *
119
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
120
	 * @param InputInterface $input Input object
121
	 */
122
	protected function setOptions( \Aimeos\MW\Config\Iface $conf, InputInterface $input )
123
	{
124
		foreach( (array) $input->getOption( 'option' ) as $option )
125
		{
126
			list( $name, $value ) = explode( ':', $option );
127
			$conf->set( str_replace( '\\', '/', $name ), $value );
128
		}
129
	}
130
}
131