Completed
Pull Request — master (#61)
by Dima
07:08
created

DiffTableHelper   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 68.33%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 34
lcom 1
cbo 4
dl 0
loc 223
ccs 82
cts 120
cp 0.6833
rs 9.2
c 3
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D addHighlighting() 0 96 25
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 Symfony\Component\Console\Helper\TableSeparator;
9
10
//----------------------------------------------------------------------------------------------------------------------
11
/**
12
 * A helper class for creating printing Tables.
13
 */
14
class DiffTableHelper
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Audit columns from config file.
19
   *
20
   * @var array
21
   */
22
  private $auditColumns;
23
24
  /**
25
   * Audit options from audit schema.
26
   *
27
   * @var array
28
   */
29
  private $auditTableOptions;
30
31
  /**
32
   * Audit options from data schema.
33
   *
34
   * @var array
35
   */
36
  private $dataTableOptions;
37
38
  /**
39
   * Check existing separator.
40
   *
41
   * @var bool
42
   */
43
  private $existSeparator = false;
44
45
  /**
46
   * Full option.
47
   *
48
   * @var bool
49
   */
50
  private $fullOption;
51
52
  /**
53
   * Array with rows for table.
54
   *
55
   * @var \array[]
56
   */
57
  private $rows = [];
58
59
  //--------------------------------------------------------------------------------------------------------------------
60
  /**
61
   * Object constructor.
62
   *
63
   * @param string  $dataSchema      Data schema name.
64
   * @param string  $auditSchema     Audit schema name.
65
   * @param string  $tableName       The table name.
66
   * @param array[] $theAuditColumns Audit columns from config file.
67
   * @param bool    $fullOption      If set append table options to rows.
68
   */
69 4
  public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption)
70
  {
71 4
    $this->auditColumns      = $theAuditColumns;
72 4
    $this->fullOption        = $fullOption;
73 4
    $this->auditTableOptions = AuditDataLayer::getTableOptions($auditSchema, $tableName);
74 4
    $this->dataTableOptions  = AuditDataLayer::getTableOptions($dataSchema, $tableName);
75 4
  }
76
77
  //--------------------------------------------------------------------------------------------------------------------
78
  /**
79
   * Add highlighting to columns.
80
   */
81 4
  public function addHighlighting()
82
  {
83 4
    $styledColumns = [];
84 4
    foreach ($this->rows as $key => $column)
85
    {
86 4
      $styledColumn = $column;
87 4
      if (is_array($column))
88 4
      {
89 4
        $auditColumn = AuditDataLayer::searchInRowSet('column_name', $column['column_name'], $this->auditColumns);
90
        // Highlighting for data table column types and audit.
91 4
        if (!empty($column['data']))
92 4
        {
93 3
          if (isset($column['data']) && !isset($column['audit']))
94 3
          {
95 1
            if (!isset($column['column_name']))
96 1
            {
97
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
98
            }
99 1
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
100 1
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
101 1
          }
102 2
          else if (!isset($column['data']) && isset($column['audit']))
103 2
          {
104
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
105
          }
106 2
          else if (strcmp($column['data'], $column['audit']))
107 2
          {
108 2
            if (!isset($column['column_name']))
109 2
            {
110 2
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
111 2
            }
112 2
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
113 2
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
114 2
            $styledColumn['audit']       = sprintf('<mm_type>%s</>', $styledColumn['audit']);
115 2
          }
116 3
          if(isset($styledColumn['config']))
117 3
          {
118 2
            if (strcmp($styledColumn['audit'], $styledColumn['config']))
119 2
            {
120 2
              if (!isset($column['column_name']))
121 2
              {
122 2
                $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
123 2
              }
124 2
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
125 2
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
126 2
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
127 2
            }
128 2
          }
129 3
        }
130
        else
131
        {
132 1
          if (!isset($column['data']) && isset($column['audit']) && !isset($auditColumn))
133 1
          {
134 1
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
135 1
          }
136
          // Highlighting for audit table column types and audit_columns in config file.
137 1
          $searchColumn = AuditDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
138 1
          if (isset($searchColumn))
139 1
          {
140
            $configType = $this->auditColumns[$searchColumn]['column_type'];
141
            if (isset($configType) && !isset($column['audit']))
142
            {
143
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
144
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
145
            }
146
            else if (!isset($configType) && isset($column['audit']))
147
            {
148
              $styledColumn['audit'] = sprintf('<mm_type>%s</>', $column['audit']);
149
            }
150
            else if (strcmp($configType, $column['audit']))
151
            {
152
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
153
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
154
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
155
            }
156
          }
157
          else
158
          {
159 1
            if (strcmp($styledColumn['audit'], $styledColumn['config']))
160 1
            {
161 1
              if (!isset($column['column_name']))
162 1
              {
163
                $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
164
              }
165 1
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
166 1
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
167 1
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
168 1
            }
169
          }
170
        }
171 4
      }
172 4
      $styledColumns[] = $styledColumn;
173 4
    }
174
175 4
    $this->rows = $styledColumns;
176 4
  }
177
178
  //--------------------------------------------------------------------------------------------------------------------
179
  /**
180
   * Appends rows.
181
   *
182
   * @param TableColumnsMetadata $rows Rows array.
183
   */
184 4
  public function appendRows($rows)
185
  {
186
    /** @var MultiSourceColumnMetadata $rowMetadata */
187 4
    foreach ($rows->getColumns() as $columnName => $rowMetadata)
188
    {
189 4
      DiffTableRowHelper::appendRow($this->rows, $rowMetadata, $columnName);
190 4
    }
191 4
    $this->appendTableOption('engine');
192 4
    $this->appendTableOption('character_set_name', 'character set');
193 4
    $this->appendTableOption('table_collation', 'collation');
194 4
  }
195
196
  //--------------------------------------------------------------------------------------------------------------------
197
  /**
198
   * Append row with table option.
199
   *
200
   * @param string      $option The option.
201
   * @param null|string $name   Display name.
202
   */
203 4
  public function appendTableOption($option, $name = null)
204
  {
205 4
    if ($this->dataTableOptions[$option]!=$this->auditTableOptions[$option] || $this->fullOption)
206 4
    {
207
      if (!$this->existSeparator)
208
      {
209
        $this->rows[]         = new TableSeparator();
210
        $this->existSeparator = true;
211
      }
212
      if ($name===null)
213
      {
214
        $name = $option;
215
      }
216
      $tableRow            = ['column_name' => $name,
217
                              'data'        => $this->dataTableOptions[$option],
218
                              'audit'       => $this->auditTableOptions[$option],
219
                              'config'      => null];
220
      $this->rows[$option] = $tableRow;
221
    }
222 4
  }
223
224
  //--------------------------------------------------------------------------------------------------------------------
225
  /**
226
   * Get rows.
227
   *
228
   * @return \array[]
229
   */
230 4
  public function getRows()
231
  {
232 4
    return $this->rows;
233
  }
234
235
  //--------------------------------------------------------------------------------------------------------------------
236
}
237
238
//----------------------------------------------------------------------------------------------------------------------
239