Completed
Push — master ( 6f8588...d5f400 )
by Aimeos
19:59
created

SetupCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 5.51 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 7
loc 127
rs 10
c 4
b 0
f 0
wmc 13
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDbConfig() 0 13 3
A setOptions() 0 8 2
A getOptions() 0 8 1
A getArguments() 7 7 1
B handle() 0 37 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 console command name.
26
	 *
27
	 * @var string
28
	 */
29
	protected $name = 'aimeos:setup';
30
31
	/**
32
	 * The console command description.
33
	 *
34
	 * @var string
35
	 */
36
	protected $description = 'Initialize or update the Aimeos database tables';
37
38
39
	/**
40
	 * Execute the console command.
41
	 *
42
	 * @return mixed
43
	 */
44
	public function handle()
45
	{
46
		$ctx = $this->getLaravel()->make( '\Aimeos\Shop\Base\Context' )->get( false, 'command' );
47
		$ctx->setEditor( 'aimeos:setup' );
48
49
		$config = $ctx->getConfig();
50
		$site = $this->argument( 'site' );
51
		$template = $this->argument( 'tplsite' );
52
53
		$config->set( 'setup/site', $site );
54
		$dbconfig = $this->getDbConfig( $config );
55
		$this->setOptions( $config );
56
57
		$taskPaths = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get()->getSetupPaths( $template );
58
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx );
59
60
		$this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) );
61
62
		if( ( $task = $this->option( 'task' ) ) && is_array( $task ) ) {
63
			$task = reset( $task );
64
		}
65
66
		switch( $this->option( 'action' ) )
67
		{
68
			case 'migrate':
69
				$manager->migrate( $task );
70
				break;
71
			case 'rollback':
72
				$manager->rollback( $task );
73
				break;
74
			case 'clean':
75
				$manager->clean( $task );
76
				break;
77
			default:
78
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $this->option( 'action' ) ) );
79
		}
80
	}
81
82
83
	/**
84
	 * Get the console command arguments.
85
	 *
86
	 * @return array
87
	 */
88 View Code Duplication
	protected function getArguments()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
	{
90
		return array(
91
			array( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' ),
92
			array( 'tplsite', InputArgument::OPTIONAL, 'Site used as template for creating the new one', 'default' ),
93
		);
94
	}
95
96
97
	/**
98
	 * Get the console command options.
99
	 *
100
	 * @return array
101
	 */
102
	protected function getOptions()
103
	{
104
		return array(
105
			array( 'option', null, InputOption::VALUE_REQUIRED, 'Setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() ),
106
			array( 'action', null, InputOption::VALUE_REQUIRED, 'Action name that should be executed, i.e. "migrate", "rollback", "clean"', 'migrate' ),
107
			array( 'task', null, InputOption::VALUE_REQUIRED, 'Name of the setup task that should be executed', null ),
108
		);
109
	}
110
111
112
	/**
113
	 * Returns the database configuration from the config object.
114
	 *
115
	 * @param \Aimeos\MW\Config\Iface $conf Config object
116
	 * @return array Multi-dimensional associative list of database configuration parameters
117
	 */
118
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
119
	{
120
		$dbconfig = $conf->get( 'resource', array() );
121
122
		foreach( $dbconfig as $rname => $dbconf )
123
		{
124
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
125
				unset( $dbconfig[$rname] );
126
			}
127
		}
128
129
		return $dbconfig;
130
	}
131
132
133
	/**
134
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
135
	 *
136
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
137
	 * @param array Associative list of database configurations
138
	 * @throws \RuntimeException If the format of the options is invalid
139
	 */
140
	protected function setOptions( \Aimeos\MW\Config\Iface $conf )
141
	{
142
		foreach( (array) $this->option( 'option' ) as $option )
143
		{
144
			list( $name, $value ) = explode( ':', $option );
145
			$conf->set( str_replace( '\\', '/', $name ), $value );
146
		}
147
	}
148
}
149