Completed
Pull Request — master (#52)
by Dima
03:17
created

DiffTableHelper   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 210
ccs 0
cts 107
cp 0
rs 9.8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D addHighlighting() 0 83 22
A appendRows() 0 11 2
B appendTableOption() 0 20 5
A getRows() 0 4 1
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
use SetBased\Audit\MySql\AuditDataLayer;
6
use SetBased\Audit\MySql\Metadata\MultiSourceColumnMetadata;
7
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata;
8
use SetBased\Stratum\MySql\StaticDataLayer;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
11
//----------------------------------------------------------------------------------------------------------------------
12
/**
13
 * A helper class for creating printing Tables.
14
 */
15
class DiffTableHelper
16
{
17
  //--------------------------------------------------------------------------------------------------------------------
18
  /**
19
   * Audit columns from config file.
20
   *
21
   * @var array
22
   */
23
  private $auditColumns;
24
25
  /**
26
   * Audit options from audit schema.
27
   *
28
   * @var array
29
   */
30
  private $auditTableOptions;
31
32
  /**
33
   * Audit options from data schema.
34
   *
35
   * @var array
36
   */
37
  private $dataTableOptions;
38
39
  /**
40
   * Check existing separator.
41
   *
42
   * @var bool
43
   */
44
  private $existSeparator = false;
45
46
  /**
47
   * Full option.
48
   *
49
   * @var bool
50
   */
51
  private $fullOption;
52
53
  /**
54
   * Array with rows for table.
55
   *
56
   * @var \array[]
57
   */
58
  private $rows = [];
59
60
  //--------------------------------------------------------------------------------------------------------------------
61
  /**
62
   * Object constructor.
63
   *
64
   * @param string  $dataSchema      Data schema name.
65
   * @param string  $auditSchema     Audit schema name.
66
   * @param string  $tableName       The table name.
67
   * @param array[] $theAuditColumns Audit columns from config file.
68
   * @param bool    $fullOption      If set append table options to rows.
69
   */
70
  public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption)
71
  {
72
    $this->auditColumns      = $theAuditColumns;
73
    $this->fullOption        = $fullOption;
74
    $this->auditTableOptions = AuditDataLayer::getTableOptions($auditSchema, $tableName);
75
    $this->dataTableOptions  = AuditDataLayer::getTableOptions($dataSchema, $tableName);
76
  }
77
78
  //--------------------------------------------------------------------------------------------------------------------
79
  /**
80
   * Add highlighting to columns.
81
   */
82
  public function addHighlighting()
83
  {
84
    $styledColumns = [];
85
    foreach ($this->rows as $key => $column)
86
    {
87
      $styledColumn = $column;
88
      if (is_array($column))
89
      {
90
        $auditColumn = StaticDataLayer::searchInRowSet('column_name', $column['column_name'], $this->auditColumns);
91
        // Highlighting for data table column types and audit.
92
        if (!empty($column['data']))
93
        {
94
          if (isset($column['data']) && !isset($column['audit']))
95
          {
96
            if (!isset($column['column_name']))
97
            {
98
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
99
            }
100
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
101
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
102
          }
103
          else if (!isset($column['data']) && isset($column['audit']))
104
          {
105
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
106
          }
107
          else if (strcmp($column['data'], $column['audit']))
108
          {
109
            if (!isset($column['column_name']))
110
            {
111
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
112
            }
113
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
114
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
115
            $styledColumn['audit']       = sprintf('<mm_type>%s</>', $styledColumn['audit']);
116
          }
117
        }
118
        else
119
        {
120
          if (!isset($column['data']) && isset($column['audit']) && !isset($auditColumn))
121
          {
122
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
123
          }
124
          // Highlighting for audit table column types and audit_columns in config file.
125
          $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
126
          if (isset($searchColumn))
127
          {
128
            $configType = $this->auditColumns[$searchColumn]['column_type'];
129
            if (isset($configType) && !isset($column['audit']))
130
            {
131
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
132
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
133
            }
134
            else if (!isset($configType) && isset($column['audit']))
135
            {
136
              $styledColumn['audit'] = sprintf('<mm_type>%s</>', $column['audit']);
137
            }
138
            else if (strcmp($configType, $column['audit']))
139
            {
140
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
141
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
142
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
143
            }
144
          }
145
          else
146
          {
147
            if (strcmp($styledColumn['audit'], $styledColumn['config']))
148
            {
149
              if (!isset($column['column_name']))
150
              {
151
                $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
152
              }
153
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
154
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
155
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
156
            }
157
          }
158
        }
159
      }
160
      $styledColumns[] = $styledColumn;
161
    }
162
163
    $this->rows = $styledColumns;
164
  }
165
166
  //--------------------------------------------------------------------------------------------------------------------
167
  /**
168
   * Appends rows.
169
   *
170
   * @param TableColumnsMetadata $theRows Rows array.
171
   */
172
  public function appendRows($theRows)
173
  {
174
    /** @var MultiSourceColumnMetadata $rowMetadata */
175
    foreach ($theRows->getColumns() as $columnName => $rowMetadata)
176
    {
177
      DiffTableRowHelper::appendRow($this->rows, $rowMetadata, $columnName);
1 ignored issue
show
Compatibility introduced by
$rowMetadata of type object<SetBased\Audit\My...etadata\ColumnMetadata> is not a sub-type of object<SetBased\Audit\My...tiSourceColumnMetadata>. It seems like you assume a child class of the class SetBased\Audit\MySql\Metadata\ColumnMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
178
    }
179
    $this->appendTableOption('engine');
180
    $this->appendTableOption('character_set_name', 'character set');
181
    $this->appendTableOption('table_collation', 'collation');
182
  }
183
184
  //--------------------------------------------------------------------------------------------------------------------
185
  /**
186
   * Append row with table option.
187
   *
188
   * @param string      $theOption The option.
189
   * @param null|string $theName   Display name.
190
   */
191
  public function appendTableOption($theOption, $theName = null)
192
  {
193
    if ($this->dataTableOptions[$theOption]!=$this->auditTableOptions[$theOption] || $this->fullOption)
194
    {
195
      if (!$this->existSeparator)
196
      {
197
        $this->rows[]         = new TableSeparator();
198
        $this->existSeparator = true;
199
      }
200
      if ($theName===null)
201
      {
202
        $theName = $theOption;
203
      }
204
      $tableRow               = ['column_name' => $theName,
205
                                 'data'        => $this->dataTableOptions[$theOption],
206
                                 'audit'       => $this->auditTableOptions[$theOption],
207
                                 'config'      => null];
208
      $this->rows[$theOption] = $tableRow;
209
    }
210
  }
211
212
  //--------------------------------------------------------------------------------------------------------------------
213
  /**
214
   * Get rows.
215
   *
216
   * @return \array[]
217
   */
218
  public function getRows()
219
  {
220
    return $this->rows;
221
  }
222
223
  //--------------------------------------------------------------------------------------------------------------------
224
}
225
226
//----------------------------------------------------------------------------------------------------------------------
227