Completed
Push — master ( c95337...5e0152 )
by P.R.
04:34
created

DiffTableHelper::getRows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
      {
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
        {
93 3
          if (isset($column['data']) && !isset($column['audit']))
94
          {
95 1
            if (!isset($column['column_name']))
96
            {
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
          }
102 2
          else if (!isset($column['data']) && isset($column['audit']))
103
          {
104
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
105
          }
106 2
          else if (strcmp($column['data'], $column['audit']))
107
          {
108 2
            if (!isset($column['column_name']))
109
            {
110 2
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
111
            }
112 2
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
113 2
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
114 3
            $styledColumn['audit']       = sprintf('<mm_type>%s</>', $styledColumn['audit']);
115
          }
116
        }
117
        else
118
        {
119 1
          if (!isset($column['data']) && isset($column['audit']) && !isset($auditColumn))
120
          {
121 1
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
122
          }
123
          // Highlighting for audit table column types and audit_columns in config file.
124 1
          $searchColumn = AuditDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
125 1
          if (isset($searchColumn))
126
          {
127
            $configType = $this->auditColumns[$searchColumn]['column_type'];
128
            if (isset($configType) && !isset($column['audit']))
129
            {
130
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
131
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
132
            }
133
            else if (!isset($configType) && isset($column['audit']))
134
            {
135
              $styledColumn['audit'] = sprintf('<mm_type>%s</>', $column['audit']);
136
            }
137
            else if (strcmp($configType, $column['audit']))
138
            {
139
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
140
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
141
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
142
            }
143
          }
144
          else
145
          {
146 1
            if (strcmp($styledColumn['audit'], $styledColumn['config']))
147
            {
148 1
              if (!isset($column['column_name']))
149
              {
150
                $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
151
              }
152 1
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
153 1
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
154 1
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
155
            }
156
          }
157
        }
158
      }
159 4
      $styledColumns[] = $styledColumn;
160
    }
161
162 4
    $this->rows = $styledColumns;
163 4
  }
164
165
  //--------------------------------------------------------------------------------------------------------------------
166
  /**
167
   * Appends rows.
168
   *
169
   * @param TableColumnsMetadata $rows Rows array.
170
   */
171 4
  public function appendRows($rows)
172
  {
173
    /** @var MultiSourceColumnMetadata $rowMetadata */
174 4
    foreach ($rows->getColumns() as $columnName => $rowMetadata)
175
    {
176 4
      DiffTableRowHelper::appendRow($this->rows, $rowMetadata, $columnName);
177
    }
178 4
    $this->appendTableOption('engine');
179 4
    $this->appendTableOption('character_set_name', 'character set');
180 4
    $this->appendTableOption('table_collation', 'collation');
181 4
  }
182
183
  //--------------------------------------------------------------------------------------------------------------------
184
  /**
185
   * Append row with table option.
186
   *
187
   * @param string      $option The option.
188
   * @param null|string $name   Display name.
189
   */
190 4
  public function appendTableOption($option, $name = null)
191
  {
192 4
    if ($this->dataTableOptions[$option]!=$this->auditTableOptions[$option] || $this->fullOption)
193
    {
194
      if (!$this->existSeparator)
195
      {
196
        $this->rows[]         = new TableSeparator();
197
        $this->existSeparator = true;
198
      }
199
      if ($name===null)
200
      {
201
        $name = $option;
202
      }
203
      $tableRow            = ['column_name' => $name,
204
                              'data'        => $this->dataTableOptions[$option],
205
                              'audit'       => $this->auditTableOptions[$option],
206
                              'config'      => null];
207
      $this->rows[$option] = $tableRow;
208
    }
209 4
  }
210
211
  //--------------------------------------------------------------------------------------------------------------------
212
  /**
213
   * Get rows.
214
   *
215
   * @return \array[]
216
   */
217 4
  public function getRows()
218
  {
219 4
    return $this->rows;
220
  }
221
222
  //--------------------------------------------------------------------------------------------------------------------
223
}
224
225
//----------------------------------------------------------------------------------------------------------------------
226