Completed
Push — master ( 7a9ce5...aad2c9 )
by Gregory
01:53
created

DumpCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 7
dl 0
loc 123
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
D execute() 0 96 10
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\InputArgument;
8
use Symfony\Component\Console\Input\InputOption;
9
use Tivnet\Console\Style;
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( 'flag', 'f', InputOption::VALUE_NONE, 'Raise a flag' ),
28
			     new InputArgument( 'activities', InputArgument::IS_ARRAY, 'Space-separated activities to perform', null ),
29
		     ) )
30
		     ->setHelp( /** @lang text */
31
			     'The <info>dump</info> runs `mysqladmin` to dump the entire database to a file in the `data` folder' );
32
	}
33
34
	/**
35
	 * Executes the command
36
	 *
37
	 * @param InputInterface  $input
38
	 * @param OutputInterface $output
39
	 *
40
	 * @return null|int
41
	 */
42
	protected function execute( InputInterface $input, OutputInterface $output ) {
43
		$io = new Style( $input, $output );
44
		$io->title( $this->getDescription() );
45
46
		$dump_folder = implode( '/', array(
47
			I::$cfg->get( 'dir_dump' ),
48
			I::$cfg->get( '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
		$cmd = implode( ' ', array(
64
			'mysqldump',
65
			I::$cfg->get( 'mysql_authorization' ),
66
			$output->isDebug() ? '--verbose' : '',
67
			'--hex-blob --no-create-db --extended-insert=FALSE --add-drop-table --quick',
68
			I::$cfg->get( 'dump_ignore' ),
69
			'--result-file',
70
			$file_dump,
71
			I::$cfg->get( 'DB_NAME' )
72
		) );
73
74
		if ( $output->isDebug() ) {
75
			$io->note( $cmd );
76
		}
77
78
		system( $cmd );
79
80
		if ( ! is_file( $file_dump ) ) {
81
			$io->error( 'The dump file cannot be found: ' . $file_dump );
82
83
			return 1;
84
		}
85
86
		if ( $io->getVerbosity() >= Style::MIN_VERBOSITY ) {
87
			$io->section( 'The dump file:' );
88
			system( I::$cfg->get( 'cmd_ls' ) . ' ' . $file_dump );
89
			system( I::$cfg->get( 'cmd_tail' ) . ' ' . $file_dump );
90
		}
91
92
		$file_dump_xz = "${file_dump}.xz";
93
94
		$xz_option_quiet = ( $io->getVerbosity() < Style::MIN_VERBOSITY ? 'q' : '' );
95
96
		$xz_options_compress = '-f' . $xz_option_quiet;
97
		$xz_options_verify   = '-t' . $xz_option_quiet;
98
99
		$io->section( 'Compressing' );
100
		system( implode( ' ', array(
101
			I::$cfg->get( 'cmd_xz' ),
102
			$xz_options_compress,
103
			$file_dump
104
		) ) );
105
106
		$io->section( 'Verifying' );
107
		$cmd_return = 0;
108
		system( implode( ' ', array(
109
			I::$cfg->get( 'cmd_xz' ),
110
			$xz_options_verify,
111
			$file_dump_xz
112
		) ), $cmd_return );
113
		if ( $cmd_return ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cmd_return of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
114
			$io->error( "Verify failed. The return code is $cmd_return" );
115
116
			return 1;
117
		}
118
119
		if ( ! $output->isQuiet() ) {
120
			$io->section( 'Done.' );
121
			system( implode( ' ', array(
122
				I::$cfg->get( 'cmd_ls' ),
123
				$file_dump_xz
124
			) ) );
125
		}
126
127
		/*
128
		 * Without `ls
129
				$output->writeln( implode( ' ', array(
130
					filesize( $file_dump ),
131
					date( 'Y-m-d H:i', filemtime( $file_dump ) ),
132
					$file_dump,
133
				) ) );
134
		*/
135
136
		return null;
137
	}
138
}
139