AlterAuditTableCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 24
ccs 11
cts 12
cp 0.9167
crap 2.0023
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Audit\Command;
5
6
use SetBased\Audit\Audit\AlterAuditTable;
7
use SetBased\Audit\Style\AuditStyle;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Command for comparing data tables with audit tables.
14
 */
15
class AlterAuditTableCommand extends AuditCommand
16
{
17
  //--------------------------------------------------------------------------------------------------------------------
18
  /**
19
   * @inheritdoc
20
   */
21 8
  protected function configure()
22
  {
23 8
    $this->setName('alter-audit-table')
24 8
         ->setDescription('Creates alter SQL statements for audit tables')
25 8
         ->addArgument('config file', InputArgument::REQUIRED, 'The audit configuration file')
26 8
         ->addArgument('sql file', InputArgument::OPTIONAL, 'The destination file for the SQL statements');
27
28 8
    $this->setHelp(<<<EOL
29 8
Generates alter table SQL statements for aligning the audit tables with the 
30
audit configuration file and data tables.
31
32
Manual inspection of the generated SQL statements is required. For example: 
33
changing an audit column from varchar(20) character set utf8 to varchar(10)
34
character set ascii might cause problems when the audit column has values
35
longer than 10 characters or values outside the ASCII character set (even 
36
though the current data table hold only values with length 10 or less and 
37
only in the ASCII character set).
38
39
No SQL statements will be generated for missing or obsolete columns in the 
40
audit tables. Use the command 'audit' for creating missing columns in audit
41
tables.
42
EOL
43
    );
44 8
  }
45
46
  //--------------------------------------------------------------------------------------------------------------------
47
  /**
48
   * @inheritdoc
49
   */
50 8
  protected function execute(InputInterface $input, OutputInterface $output): int
51
  {
52 8
    $this->io = new AuditStyle($input, $output);
53
54 8
    $sqlFilename = $input->getArgument('sql file');
55
56 8
    $this->configFileName = $input->getArgument('config file');
57 8
    $this->readConfigFile();
58
59 8
    $this->connect();
60
61 8
    $alter = new AlterAuditTable($this->config);
62 8
    $sql   = $alter->main();
63
64 8
    if ($sqlFilename!==null)
65
    {
66 8
      $this->writeTwoPhases($sqlFilename, $sql);
67
    }
68
    else
69
    {
70
      $this->io->write($sql);
71
    }
72
73 8
    return 0;
74
  }
75
76
  //--------------------------------------------------------------------------------------------------------------------
77
}
78
79
//----------------------------------------------------------------------------------------------------------------------
80