Completed
Pull Request — master (#55)
by Dima
03:18
created

AuditDiffTable::compareCollationCharSetName()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.216

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 17
cts 20
cp 0.85
rs 5.3846
cc 8
eloc 17
nc 4
nop 3
crap 8.216
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql;
4
5
use SetBased\Audit\MySql\Helper\DiffTableColumns;
6
use SetBased\Audit\MySql\Metadata\MultiSourceColumnMetadata;
7
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata;
8
use SetBased\Stratum\MySql\StaticDataLayer;
9
10
//----------------------------------------------------------------------------------------------------------------------
11
/**
12
 * Class for executing auditing actions for tables.
13
 */
14
class AuditDiffTable
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Audit database schema.
19
   *
20
   * @var string
21
   */
22
  private $auditSchema;
23
24
  /**
25
   * Audit columns from config file.
26
   *
27
   * @var array[]
28
   */
29
  private $configAuditColumns;
30
31
  /**
32
   * Audit columns from config file
33
   *
34
   * @var array[]
35
   */
36
  private $configColumns;
37
38
  /**
39
   * Data database schema.
40
   *
41
   * @var string
42
   */
43
  private $dataSchema;
44
45
  /**
46
   * Difference between data and audit tables.
47
   *
48
   * @var DiffTableColumns
49
   */
50
  private $diffColumns;
51
52
  /**
53
   * Table name.
54
   *
55
   * @var string
56
   */
57
  private $tableName;
58
59
  //--------------------------------------------------------------------------------------------------------------------
60
  /**
61
   * Object constructor.
62
   *
63
   * @param string  $dataSchema         Data database schema.
64
   * @param string  $auditSchema        Audit database schema.
65
   * @param string  $tableName          Table name.
66
   * @param array[] $configAuditColumns Audit columns from config file.
67
   * @param array[] $configColumns      Data columns from config file.
68
   */
69 4
  public function __construct($dataSchema, $auditSchema, $tableName, $configAuditColumns, $configColumns)
70
  {
71 4
    $this->dataSchema         = $dataSchema;
72 4
    $this->auditSchema        = $auditSchema;
73 4
    $this->tableName          = $tableName;
74 4
    $this->configAuditColumns = $configAuditColumns;
75 4
    $this->configColumns      = array_merge($configAuditColumns, $configColumns);
76
77 4
    $dataColumns  = new TableColumnsMetadata(AuditDataLayer::getTableColumns($this->dataSchema, $this->tableName));
78 4
    $auditColumns = AuditDataLayer::getTableColumns($this->auditSchema, $this->tableName);
79 4
    $auditColumns = $this->addNotNull($auditColumns);
80 4
    $auditColumns = new TableColumnsMetadata($auditColumns);
81
82 4
    $this->createDiffArray($dataColumns, $auditColumns);
83 4
  }
84
85
  //--------------------------------------------------------------------------------------------------------------------
86
  /**
87
   * Return diff columns.
88
   *
89
   * @return TableColumnsMetadata
90
   */
91 4
  public function getDiffColumns()
92
  {
93 4
    return $this->diffColumns->getColumns();
94
  }
95
96
  //--------------------------------------------------------------------------------------------------------------------
97
  /**
98
   * Check full and return array without new or obsolete columns if full not set.
99
   *
100
   * @return TableColumnsMetadata
101
   */
102 4
  public function removeMatchingColumns()
103
  {
104 4
    $metadata = $this->diffColumns->getColumns();
105
    /** @var MultiSourceColumnMetadata $column */
106 4
    foreach ($metadata->getColumns() as $columnName => $column)
107
    {
108
      /** @var MultiSourceColumnMetadata $data */
109 4
      $data = $column->getProperty('data');
110
      /** @var MultiSourceColumnMetadata $audit */
111 4
      $audit = $column->getProperty('audit');
112
      /** @var MultiSourceColumnMetadata $config */
113 4
      $config = $column->getProperty('config');
114
115 4
      $auditColumn = StaticDataLayer::searchInRowSet('column_name', $columnName, $this->configAuditColumns);
116 4
      if (!isset($data))
117 4
      {
118 1
        if (isset($audit) && isset($config))
119 1
        {
120
          $check = $this->compareCollationCharSetName($audit, $config);
121
          if ($audit->getProperty('column_type')==$config->getProperty('column_type') && $check)
122
          {
123
            if (isset($auditColumn))
124
            {
125
              $metadata->removeColumn($columnName);
126
            }
127
          }
128
        }
129 1
      }
130
      else
131
      {
132 4
        if (isset($audit) && isset($config))
133 4
        {
134 4
          $check = $this->compareCollationCharSetName($data, $audit, $config);
135 4
          if ($audit->getProperty('column_type')==$data->getProperty('column_type') && $check)
136 4
          {
137 4
            $metadata->removeColumn($columnName);
138 4
          }
139 4
        }
140
      }
141 4
    }
142
143 4
    return $metadata;
144
  }
145
146
  //--------------------------------------------------------------------------------------------------------------------
147
  /**
148
   * Add not null to audit columns if it not nullable.
149
   *
150
   * @param array $theColumns Audit columns.
151
   *
152
   * @return array
153
   */
154 4
  private function addNotNull($theColumns)
155
  {
156 4
    $modifiedColumns = [];
157 4
    foreach ($theColumns as $column)
158
    {
159 4
      $modifiedColumn = $column;
160 4
      $auditColumn    = StaticDataLayer::searchInRowSet('column_name', $modifiedColumn['column_name'], $this->configColumns);
161 4
      if (isset($auditColumn))
162 4
      {
163 4
        if ($modifiedColumn['is_nullable']==='NO')
164 4
        {
165
          $modifiedColumn['column_type'] = sprintf('%s not null', $modifiedColumn['column_type']);
166
        }
167 4
      }
168 4
      $modifiedColumns[] = $modifiedColumn;
169 4
    }
170
171 4
    return $modifiedColumns;
172
  }
173
174
  //--------------------------------------------------------------------------------------------------------------------
175
  /**
176
   * Compare collations and character set names.
177
   *
178
   * @param MultiSourceColumnMetadata      $audit  Column information from audit schema.
179
   * @param MultiSourceColumnMetadata      $config Column information from config file.
180
   * @param MultiSourceColumnMetadata|null $data   Column information from data schema.
181
   *
182
   * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
183
   */
184 4
  private function compareCollationCharSetName($audit, $config, $data = null)
185
  {
186 4
    $config_character_set_name = $config->getProperty('character_set_name');
187 4
    $config_collation_name     = $config->getProperty('collation_name');
188 4
    $audit_character_set_name  = $audit->getProperty('character_set_name');
189 4
    $audit_collation_name      = $audit->getProperty('collation_name');
190
191 4
    if (isset($data))
192 4
    {
193 4
      $data_character_set_name = $data->getProperty('character_set_name');
194 4
      $data_collation_name     = $data->getProperty('collation_name');
195
196
      if ($audit_character_set_name==$data_character_set_name
197 4
        && $audit_character_set_name==$config_character_set_name
198 4
        && $audit_collation_name==$config_collation_name
199 4
        && $audit_collation_name==$data_collation_name
200 4
      )
201 4
      {
202 4
        return true;
203
      }
204 2
    }
205
    else
206
    {
207
      if ($audit_character_set_name==$config_character_set_name && $audit_collation_name==$config_collation_name)
208
      {
209
        return true;
210
      }
211
    }
212
213 2
    return false;
214
  }
215
216
  //--------------------------------------------------------------------------------------------------------------------
217
  /**
218
   * Get the difference between data and audit tables.
219
   *
220
   * @param TableColumnsMetadata $dataColumns  The table columns from data schema.
221
   * @param TableColumnsMetadata $auditColumns The table columns from audit schema.
222
   */
223 4
  private function createDiffArray($dataColumns, $auditColumns)
224
  {
225 4
    $configColumns     = new TableColumnsMetadata($this->configColumns);
0 ignored issues
show
Documentation introduced by
$this->configColumns 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...
226 4
    $this->diffColumns = new DiffTableColumns($configColumns, $auditColumns, $dataColumns);
227 4
  }
228
229
  //--------------------------------------------------------------------------------------------------------------------
230
}
231
232
//----------------------------------------------------------------------------------------------------------------------
233