Completed
Push — master ( c95337...5e0152 )
by P.R.
04:34
created

DropTriggersCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 28%

Importance

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

3 Methods

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