Completed
Branch master (0ecb66)
by P.R.
04:50
created

DropTriggersCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 28%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 2
cbo 4
dl 0
loc 56
ccs 7
cts 25
cp 0.28
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 16 1
A dropTriggers() 0 13 2
1
<?php
2
3
namespace SetBased\Audit\MySql\Command;
4
5
use SetBased\Audit\MySql\AuditDataLayer;
6
use SetBased\Stratum\Style\StratumStyle;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Command for dropping all triggers.
13
 */
14
class DropTriggersCommand extends MySqlBaseCommand
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * @inheritdoc
19
   */
20 1
  protected function configure()
21
  {
22 1
    $this->setName('drop-triggers')
23 1
         ->setDescription('Drops all triggers')
24 1
         ->setHelp('Drops all triggers (including triggers not created by audit) from all tables (including tables '.
25 1
                   'excluded for auditing) in the data schema.')
26 1
         ->addArgument('config file', InputArgument::REQUIRED, 'The audit configuration file');
27 1
  }
28
29
  //--------------------------------------------------------------------------------------------------------------------
30
  /**
31
   * @inheritdoc
32
   */
33
  protected function execute(InputInterface $input, OutputInterface $output)
34
  {
35
    $this->io = new StratumStyle($input, $output);
36
37
    $this->configFileName = $input->getArgument('config file');
38
    $this->readConfigFile();
39
40
    // Create database connection with params from config file
41
    $this->connect($this->config);
42
43
    $this->dropTriggers();
44
45
    AuditDataLayer::disconnect();
46
47
    $this->rewriteConfig();
48
  }
49
50
  //--------------------------------------------------------------------------------------------------------------------
51
  /**
52
   * Drops all triggers.
53
   */
54
  private function dropTriggers()
55
  {
56
    $data_schema = $this->config['database']['data_schema'];
57
    $triggers    = AuditDataLayer::getTriggers($data_schema);
58
    foreach ($triggers as $trigger)
59
    {
60
      $this->io->logInfo('Dropping trigger <dbo>%s</dbo> from table <dbo>%s</dbo>',
61
                         $trigger['trigger_name'],
62
                         $trigger['table_name']);
63
64
      AuditDataLayer::dropTrigger($data_schema, $trigger['trigger_name']);
65
    }
66
  }
67
68
  //--------------------------------------------------------------------------------------------------------------------
69
}
70
71
//----------------------------------------------------------------------------------------------------------------------
72