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

DumpCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 7
dl 0
loc 128
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

2 Methods

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