Completed
Pull Request — master (#40)
by Dima
03:24
created

AuditDiffTable::addNotNull()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 20
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql;
4
5
use SetBased\Audit\MySql\Helper\DiffTableColumns;
6
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata;
7
use SetBased\Stratum\MySql\StaticDataLayer;
8
9
//----------------------------------------------------------------------------------------------------------------------
10
/**
11
 * Class for executing auditing actions for tables.
12
 */
13
class AuditDiffTable
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   *
18
   *
19
   * @var array[]
20
   */
21
  private $auditColumns;
22
23
  /**
24
   * Audit database schema.
25
   *
26
   * @var string
27
   */
28
  private $auditSchema;
29
30
  /**
31
   * Data database schema.
32
   *
33
   * @var string
34
   */
35
  private $dataSchema;
36
37
  /**
38
   * Difference between data and audit tables.
39
   *
40
   * @var DiffTableColumns
41
   */
42
  private $diffColumns;
43
44
  /**
45
   * Table name.
46
   *
47
   * @var string
48
   */
49
  private $tableName;
50
51
  //--------------------------------------------------------------------------------------------------------------------
52
  /**
53
   * Object constructor.
54
   *
55
   * @param string  $dataSchema   Data database schema.
56
   * @param string  $auditSchema  Audit database schema.
57
   * @param string  $tableName    Table name.
58
   * @param array[] $auditColumns Audit columns from config file.
59
   */
60
  public function __construct($dataSchema, $auditSchema, $tableName, $auditColumns)
61
  {
62
    $this->dataSchema   = $dataSchema;
63
    $this->auditSchema  = $auditSchema;
64
    $this->tableName    = $tableName;
65
    $this->auditColumns = $auditColumns;
66
67
    $dataColumns  = new TableColumnsMetadata(AuditDataLayer::getTableColumns($this->dataSchema, $this->tableName));
68
    $auditColumns = AuditDataLayer::getTableColumns($this->auditSchema, $this->tableName);
69
    $auditColumns = $this->addNotNull($auditColumns);
70
    $auditColumns = new TableColumnsMetadata($auditColumns);
71
72
    $this->createDiffArray($dataColumns, $auditColumns);
73
  }
74
75
  //--------------------------------------------------------------------------------------------------------------------
76
  /**
77
   * Return diff columns.
78
   *
79
   * @return array[]
80
   */
81
  public function getDiffColumns()
82
  {
83
    return $this->diffColumns->getTypes();
84
  }
85
86
  //--------------------------------------------------------------------------------------------------------------------
87
  /**
88
   * Check full and return array without new or obsolete columns if full not set.   *
89
   *
90
   * @return array[]
91
   */
92
  public function removeMatchingColumns()
93
  {
94
    $cleaned = [];
95
    /** @var DiffTableColumns $column */
96
    foreach ($this->diffColumns as $column)
0 ignored issues
show
Bug introduced by
The expression $this->diffColumns of type object<SetBased\Audit\My...elper\DiffTableColumns> is not traversable.
Loading history...
97
    {
98
      $columnsArray = $column->getTypes();
99
      if (!isset($columnsArray['data_column_type']))
100
      {
101
        if ($columnsArray['audit_column_type']!=$columnsArray['config_column_type'])
102
        {
103
          $cleaned[] = $column;
104
        }
105
      }
106 View Code Duplication
      elseif (!isset($columnsArray['config_column_type']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
      {
108
        if (($columnsArray['audit_column_type']!=$columnsArray['data_column_type']) || ($columnsArray['audit_character_set_name']!=$columnsArray['data_character_set_name'] || $columnsArray['audit_collation_name']!=$columnsArray['data_collation_name']))
109
        {
110
          $cleaned[] = $column;
111
        }
112
      }
113 View Code Duplication
      else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
      {
115
        if (($columnsArray['data_column_type']!=$columnsArray['audit_column_type'] && $columnsArray['audit_column_type']!=$columnsArray['config_column_type']) || ($columnsArray['audit_column_type']!=$columnsArray['config_column_type'] && !empty($columnsArray['config_column_type'])))
116
        {
117
          $cleaned[] = $column;
118
        }
119
      }
120
    }
121
122
    return $cleaned;
123
  }
124
125
  //--------------------------------------------------------------------------------------------------------------------
126
  /**
127
   * Add not null to audit columns if it not nullable.
128
   *
129
   * @param array $theColumns Audit columns.
130
   *
131
   * @return array
132
   */
133
  private function addNotNull($theColumns)
134
  {
135
    $modifiedColumns = [];
136
    foreach ($theColumns as $column)
137
    {
138
      $modifiedColumn = $column;
139
      $auditColumn    = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->auditColumns);
140
      if (isset($auditColumn))
141
      {
142
        if ($modifiedColumn['is_nullable']==='NO')
143
        {
144
          $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']);
145
        }
146
      }
147
      $modifiedColumns[] = $modifiedColumn;
148
    }
149
150
    return $modifiedColumns;
151
  }
152
153
  //--------------------------------------------------------------------------------------------------------------------
154
  /**
155
   * Get the difference between data and audit tables.
156
   *
157
   * @param TableColumnsMetadata $dataColumns  The table columns from data schema.
158
   * @param TableColumnsMetadata $auditColumns The table columns from audit schema.
159
   */
160
  private function createDiffArray($dataColumns, $auditColumns)
161
  {
162
    $configColumns = new TableColumnsMetadata($this->auditColumns);
0 ignored issues
show
Documentation introduced by
$this->auditColumns is of type array<integer,array>, but the function expects a array<integer,object<array>>.

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...
163
    $diff          = new DiffTableColumns($configColumns, $auditColumns, $dataColumns);
164
165
    $this->diffColumns = $diff;
166
  }
167
168
  //--------------------------------------------------------------------------------------------------------------------
169
}
170
171
//----------------------------------------------------------------------------------------------------------------------
172