Completed
Pull Request — master (#41)
by Dima
05:18
created

AuditCommand::readMetadata()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3.0067
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
   *
23
   * @var array
24
   */
25
  protected $configMetadata;
26
27
  /**
28
   * File name for config metadata.
29
   *
30
   * @var array
31
   */
32
  protected $configMetadataFile;
33
34
  //--------------------------------------------------------------------------------------------------------------------
35
  /**
36
   * {@inheritdoc}
37
   */
38 14
  protected function configure()
39
  {
40 14
    $this->setName('audit')
41 14
         ->setDescription('Create (missing) audit table and (re)creates audit triggers')
42 14
         ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file');
43 14
  }
44
45
  //--------------------------------------------------------------------------------------------------------------------
46
  /**
47
   * {@inheritdoc}
48
   */
49 13
  protected function execute(InputInterface $input, OutputInterface $output)
50
  {
51 13
    $this->io = new StratumStyle($input, $output);
52
53 13
    $this->configFileName = $input->getArgument('config file');
54 13
    $this->readConfigFile();
55
56 13
    $this->readMetadata();
57
58
    // Create database connection with params from config file
59 13
    $this->connect($this->config);
60
61 13
    $audit  = new Audit($this->config, $this->configMetadata, $this->io);
62 13
    $status = $audit->main();
63
64
    // Drop database connection
65 13
    AuditDataLayer::disconnect();
66
67 13
    $this->rewriteConfig();
68
69 13
    return $status;
70
  }
71
72
  //--------------------------------------------------------------------------------------------------------------------
73
  /**
74
   * Read tables metadata from config file.
75
   */
76 13
  protected function readMetadata()
77
  {
78 13
    if (isset($this->config['metadata']))
79 13
    {
80 13
      $this->configMetadataFile = dirname($this->configFileName).'/'.$this->config['metadata'];
0 ignored issues
show
Documentation Bug introduced by
It seems like dirname($this->configFil...his->config['metadata'] of type string is incompatible with the declared type array of property $configMetadataFile.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81 13
      $content                  = file_get_contents($this->configMetadataFile);
82
83 13
      $this->configMetadata = (array)json_decode($content, true);
84 13
      if (json_last_error()!=JSON_ERROR_NONE)
85 13
      {
86
        throw new RuntimeException("Error decoding JSON: '%s'.", json_last_error_msg());
87
      }
88 13
    }
89 13
  }
90
91
  //--------------------------------------------------------------------------------------------------------------------
92
  /**
93
   * Rewrites the config file with updated data.
94
   */
95 13
  protected function rewriteConfig()
96
  {
97
    // Return immediately when the config file must not be rewritten.
98 13
    if (!$this->rewriteConfigFile) return;
99
100
    $this->writeTwoPhases($this->configFileName, json_encode($this->config, JSON_PRETTY_PRINT));
101
    $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...
102
  }
103
104
  //--------------------------------------------------------------------------------------------------------------------
105
}
106
107
//----------------------------------------------------------------------------------------------------------------------
108