Completed
Pull Request — master (#41)
by Dima
03:20
created

AuditCommand::readMetadata()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 12
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Command;
4
5
use SetBased\Audit\MySql\Audit;
6
use SetBased\Audit\MySql\AuditDataLayer;
7
use SetBased\Exception\RuntimeException;
8
use SetBased\Stratum\Style\StratumStyle;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
//----------------------------------------------------------------------------------------------------------------------
14
/**
15
 * Command for creating audit tables and audit triggers.
16
 */
17
class AuditCommand extends MySqlBaseCommand
18
{
19
  //--------------------------------------------------------------------------------------------------------------------
20
  /**
21
   * Tables metadata from config file.
22 14
   *
23
   * @var array
24 14
   */
25 14
  protected $configMetadata;
26 14
27 14
  /**
28
   * File name for config metadata.
29
   *
30
   * @var array
31
   */
32
  protected $configMetadataFile;
33 13
34
  //--------------------------------------------------------------------------------------------------------------------
35 13
  /**
36
   * {@inheritdoc}
37 13
   */
38 13
  protected function configure()
39
  {
40
    $this->setName('audit')
41 13
         ->setDescription('Create (missing) audit table and (re)creates audit triggers')
42
         ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file');
43 13
  }
44 13
45
  //--------------------------------------------------------------------------------------------------------------------
46
  /**
47 13
   * {@inheritdoc}
48
   */
49 13
  protected function execute(InputInterface $input, OutputInterface $output)
50
  {
51 13
    $this->io = new StratumStyle($input, $output);
52
53
    $this->configFileName = $input->getArgument('config file');
54
    $this->readConfigFile();
55
56
    $this->readMetadata();
57
58
    // Create database connection with params from config file
59
    $this->connect($this->config);
60
61
    $audit  = new Audit($this->config, $this->configMetadata, $this->io);
62
    $status = $audit->main();
63
64
    // Drop database connection
65
    AuditDataLayer::disconnect();
66
67
    $this->rewriteConfig();
68
69
    return $status;
70
  }
71
72
  //--------------------------------------------------------------------------------------------------------------------
73
  /**
74
   * Rewrites the config file with updated data.
75
   */
76
  protected function rewriteConfig()
77
  {
78
    // Return immediately when the config file must not be rewritten.
79
    if (!$this->rewriteConfigFile) return;
80
81
    $this->writeTwoPhases($this->configFileName, json_encode($this->config, JSON_PRETTY_PRINT));
82
    $this->writeTwoPhases($this->configMetadataFile, json_encode($this->configMetadata, JSON_PRETTY_PRINT));
0 ignored issues
show
Documentation introduced by
$this->configMetadataFile is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
  }
84
85
  //--------------------------------------------------------------------------------------------------------------------
86
  /**
87
   * Read tables metadata from config file.
88
   */
89
  protected function readMetadata()
90
  {
91
    if (isset($this->config['metadata']))
92
    {
93
      $this->configMetadataFile = $this->config['metadata'];
94
      $content                  = file_get_contents($this->configMetadataFile);
95
96
      $this->configMetadata = (array)json_decode($content, true);
97
      if (json_last_error()!=JSON_ERROR_NONE)
98
      {
99
        throw new RuntimeException("Error decoding JSON: '%s'.", json_last_error_msg());
100
      }
101
    }
102
  }
103
104
  //--------------------------------------------------------------------------------------------------------------------
105
}
106
107
//----------------------------------------------------------------------------------------------------------------------
108