DumpCommand::execute()   C
last analyzed

Complexity

Conditions 11
Paths 54

Size

Total Lines 100
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 0
cts 68
cp 0
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 58
nc 54
nop 2
crap 132

How to fix   Long Method    Complexity   

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 DumpCommand
14
 * @package Tivnet\WPDB\Command
15
 */
16
class DumpCommand extends Command {
17
18
	/**
19
	 * Configuration
20
	 *
21
	 * @return void
22
	 */
23
	protected function configure() {
24
		$this->setName( 'dump' )
25
		     ->setDescription( 'Dump database to a file' )
26
		     ->setDefinition( array(
27
			     new InputOption( 'env', 'e', InputOption::VALUE_OPTIONAL, 'Specify the environment' ),
28
		     ) )
29
		     ->setHelp( /** @lang text */
30
			     'The <info>dump</info> runs `mysqladmin` to dump the entire database to a file in the `data` 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( 'Creating the dump folder' );
53
54
		if ( ! @mkdir( $dump_folder, 0750, true ) && ! is_dir( $dump_folder ) ) {
55
			$io->error( 'Cannot create folder: ' . $dump_folder );
56
57
			return 1;
58
		}
59
60
		$io->section( 'Dumping the database' );
61
62
		$file_dump = $dump_folder . '/' . I::$cfg->get( 'dump_prefix' ) . '.' . I::$cfg->get( 'dump_ext' );
63
64
		$file_dump_escaped = escapeshellarg( $file_dump );
65
66
		$cmd = implode( ' ', array(
67
			'mysqldump',
68
			I::$cfg->get( 'mysql_authorization' ),
69
			$output->isDebug() ? '--verbose' : '',
70
			'--hex-blob --no-create-db --extended-insert=FALSE --add-drop-table --quick',
71
			I::$cfg->get( 'dump_ignore' ),
72
			'--result-file',
73
			$file_dump_escaped,
74
			I::$cfg->get( 'DB_NAME' )
75
		) );
76
77
		if ( $output->isDebug() ) {
78
			$io->note( $cmd );
79
		}
80
81
		system( $cmd );
82
83
		if ( ! is_file( $file_dump ) ) {
84
			$io->error( 'The dump file cannot be found: ' . $file_dump );
85
86
			return 1;
87
		}
88
89
		if ( $io->getVerbosity() >= Style::MIN_VERBOSITY ) {
90
			$io->section( 'The dump file:' );
91
			$io->writeln( Utils::ls_l( $file_dump ) );
92
			system( I::$cfg->get( 'cmd_tail' ) . ' ' . $file_dump_escaped );
93
		}
94
95
		$file_dump_xz = $file_dump . '.' . I::$cfg->get( 'ext_xz' );
96
		$file_dump_xz_escaped = escapeshellarg( $file_dump_xz );
97
98
		$xz_option_quiet = ( $io->getVerbosity() < Style::MIN_VERBOSITY ? 'q' : '' );
99
100
		$xz_options_compress = '-f' . $xz_option_quiet;
101
		$xz_options_verify   = '-t' . $xz_option_quiet;
102
103
		$io->section( 'Compressing' );
104
		system( implode( ' ', array(
105
			I::$cfg->get( 'cmd_xz' ),
106
			$xz_options_compress,
107
			$file_dump_escaped
108
		) ) );
109
110
		$io->section( 'Verifying' );
111
		$cmd_return = 0;
112
		system( implode( ' ', array(
113
			I::$cfg->get( 'cmd_xz' ),
114
			$xz_options_verify,
115
			$file_dump_xz_escaped
116
		) ), $cmd_return );
117
		if ( 0 !== (int) $cmd_return ) {
118
			$io->error( "Verify failed. The return code is $cmd_return" );
119
120
			return 1;
121
		}
122
123
		if ( ! $output->isQuiet() ) {
124
			$io->section( 'Done.' );
125
			$io->writeln( Utils::ls_l( $file_dump_xz ) );
126
		}
127
128
		/*
129
		 * Without `ls
130
				$output->writeln( implode( ' ', array(
131
					filesize( $file_dump ),
132
					date( 'Y-m-d H:i', filemtime( $file_dump ) ),
133
					$file_dump,
134
				) ) );
135
		*/
136
137
		$io->success( 'Done.' );
138
139
		return null;
140
	}
141
}
142