Completed
Push — master ( 66ff4f...abc7c9 )
by Gregory
02:50
created

LoadCommand::execute()   C

Complexity

Conditions 9
Paths 28

Size

Total Lines 78
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 0
cts 59
cp 0
rs 5.7191
c 0
b 0
f 0
cc 9
eloc 49
nc 28
nop 2
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Tivnet\WPDB\Command;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Tivnet\Console\Style;
9
use Tivnet\WPDB\I;
10
11
/**
12
 * Class LoadCommand
13
 * @package Tivnet\WPDB\Command
14
 */
15
class LoadCommand extends Command {
16
17
	/**
18
	 * Configuration
19
	 *
20
	 * @return void
21
	 */
22
	protected function configure() {
23
		$this->setName( 'load' )
24
		     ->setDescription( 'Load database from a dump file' )
25
		     ->setDefinition( array(
26
			     new InputOption( 'env', 'e', InputOption::VALUE_OPTIONAL, 'Specify the environment' ),
27
		     ) )
28
		     ->setHelp( /** @lang text */
29
			     'The <info>load</info> runs `mysql` to load the SQL file from the dumps folder' );
30
	}
31
32
	/**
33
	 * Executes the command
34
	 *
35
	 * @param InputInterface  $input
36
	 * @param OutputInterface $output
37
	 *
38
	 * @return null|int
39
	 */
40
	protected function execute( InputInterface $input, OutputInterface $output ) {
41
		$io = new Style( $input, $output );
42
		$io->title( $this->getDescription() );
43
44
		$env = $input->getOption( 'env' ) ?: I::$cfg->get( 'env' );
45
46
		$dump_folder = implode( '/', array(
47
			I::$cfg->get( 'dir_dump' ),
48
			$env,
49
		) );
50
51
		$io->section( 'Locating the dump file' );
52
53
		$file_dump            = $dump_folder . '/' . I::$cfg->get( 'dump_prefix' ) . '.' . I::$cfg->get( 'dump_ext' );
54
		$file_dump_escaped    = escapeshellarg( $file_dump );
55
		$file_dump_xz         = $file_dump . '.' . I::$cfg->get( 'ext_xz' );
56
		$file_dump_xz_escaped = escapeshellarg( $file_dump_xz );
57
58
		if ( ! is_file( $file_dump_xz ) ) {
59
			$io->error( 'The dump file cannot be found: ' . $file_dump_xz );
60
61
			return 1;
62
		}
63
64
		system( implode( ' ', array(
65
			I::$cfg->get( 'cmd_ls' ),
66
			$file_dump_xz_escaped
67
		) ) );
68
69
		if ( ! $io->confirm( 'Continue?', true ) ) {
70
			return null;
71
		}
72
73
		$xz_option_quiet = ( $io->getVerbosity() < Style::MIN_VERBOSITY ? 'q' : '' );
74
75
		$xz_options_decompress = '-dfk' . $xz_option_quiet;
76
77
		$io->section( 'Decompressing' );
78
		system( implode( ' ', array(
79
			I::$cfg->get( 'cmd_xz' ),
80
			$xz_options_decompress,
81
			$file_dump_xz_escaped
82
		) ) );
83
84
		$io->section( 'Loading' );
85
		$cmd = implode( ' ', array(
86
			'mysql',
87
			I::$cfg->get( 'mysql_authorization' ),
88
			'-h',
89
			I::$cfg->get( 'DB_HOST' ),
90
			$output->isDebug() ? '--verbose' : '',
91
			I::$cfg->get( 'DB_NAME' ),
92
			'<',
93
			$file_dump_escaped,
94
		) );
95
96
		if ( $output->isDebug() ) {
97
			$io->note( $cmd );
98
		}
99
100
		$cmd_return = 0;
101
		system( $cmd, $cmd_return );
102
		if ( 0 !== (int) $cmd_return ) {
103
			$io->error( "Load failed. The return code is $cmd_return" );
104
105
			return 1;
106
		}
107
108
		$io->section( 'Removing decompressed dump file' );
109
		if ( ! unlink( $file_dump ) ) {
110
			$io->error( 'Removing decompressed dump file failed: ' . $file_dump );
111
		}
112
113
		$io->success( 'Done.' );
114
115
		return null;
116
117
	}
118
}
119