Completed
Pull Request — master (#55)
by Dima
03:18
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 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 4
  public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns, $fullOption)
71
  {
72 4
    $this->auditColumns      = $theAuditColumns;
73 4
    $this->fullOption        = $fullOption;
74 4
    $this->auditTableOptions = AuditDataLayer::getTableOptions($auditSchema, $tableName);
75 4
    $this->dataTableOptions  = AuditDataLayer::getTableOptions($dataSchema, $tableName);
76 4
  }
77
78
  //--------------------------------------------------------------------------------------------------------------------
79
  /**
80
   * Add highlighting to columns.
81
   */
82 4
  public function addHighlighting()
83
  {
84 4
    $styledColumns = [];
85 4
    foreach ($this->rows as $key => $column)
86
    {
87 4
      $styledColumn = $column;
88 4
      if (is_array($column))
89 4
      {
90 4
        $auditColumn = StaticDataLayer::searchInRowSet('column_name', $column['column_name'], $this->auditColumns);
91
        // Highlighting for data table column types and audit.
92 4
        if (!empty($column['data']))
93 4
        {
94 3
          if (isset($column['data']) && !isset($column['audit']))
95 3
          {
96 1
            if (!isset($column['column_name']))
97 1
            {
98
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
99
            }
100 1
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
101 1
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
102 1
          }
103 2
          else if (!isset($column['data']) && isset($column['audit']))
104 2
          {
105
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
106
          }
107 2
          else if (strcmp($column['data'], $column['audit']))
108 2
          {
109 2
            if (!isset($column['column_name']))
110 2
            {
111 1
              $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
112 1
            }
113 2
            $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
114 2
            $styledColumn['data']        = sprintf('<mm_type>%s</>', $styledColumn['data']);
115 2
            $styledColumn['audit']       = sprintf('<mm_type>%s</>', $styledColumn['audit']);
116 2
          }
117 3
        }
118
        else
119
        {
120 2
          if (!isset($column['data']) && isset($column['audit']) && !isset($auditColumn))
121 2
          {
122 1
            $styledColumn['audit'] = sprintf('<mm_type>%s</>', $styledColumn['audit']);
123 1
          }
124
          // Highlighting for audit table column types and audit_columns in config file.
125 2
          $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
126 2
          if (isset($searchColumn))
127 2
          {
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 2
            if (strcmp($styledColumn['audit'], $styledColumn['config']))
148 2
            {
149 2
              if (!isset($column['column_name']))
150 2
              {
151 1
                $styledColumns[$key - 1]['column_name'] = sprintf('<mm_column>%s</>', $styledColumns[$key - 1]['column_name']);
152 1
              }
153 2
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
154 2
              $styledColumn['audit']       = sprintf('<mm_type>%s</>', $column['audit']);
155 2
              $styledColumn['config']      = sprintf('<mm_type>%s</>', $styledColumn['config']);
156 2
            }
157
          }
158
        }
159 4
      }
160 4
      $styledColumns[] = $styledColumn;
161 4
    }
162
163 4
    $this->rows = $styledColumns;
164 4
  }
165
166
  //--------------------------------------------------------------------------------------------------------------------
167
  /**
168
   * Appends rows.
169
   *
170
   * @param TableColumnsMetadata $theRows Rows array.
171
   */
172 4
  public function appendRows($theRows)
173
  {
174
    /** @var MultiSourceColumnMetadata $rowMetadata */
175 4
    foreach ($theRows->getColumns() as $columnName => $rowMetadata)
176
    {
177 4
      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 4
    }
179 4
    $this->appendTableOption('engine');
180 4
    $this->appendTableOption('character_set_name', 'character set');
181 4
    $this->appendTableOption('table_collation', 'collation');
182 4
  }
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 4
  public function appendTableOption($theOption, $theName = null)
192
  {
193 4
    if ($this->dataTableOptions[$theOption]!=$this->auditTableOptions[$theOption] || $this->fullOption)
194 4
    {
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 4
  }
211
212
  //--------------------------------------------------------------------------------------------------------------------
213
  /**
214
   * Get rows.
215
   *
216
   * @return \array[]
217
   */
218 4
  public function getRows()
219
  {
220 4
    return $this->rows;
221
  }
222
223
  //--------------------------------------------------------------------------------------------------------------------
224
}
225
226
//----------------------------------------------------------------------------------------------------------------------
227