LoadCommand::execute()   C
last analyzed

Complexity

Conditions 9
Paths 28

Size

Total Lines 75
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

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