Completed
Push — master ( d10df0...c2fd32 )
by P.R.
07:30
created

DiffTableHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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