Completed
Pull Request — master (#29)
by
unknown
02:32
created

TableHelper::appendRows()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
//----------------------------------------------------------------------------------------------------------------------
6
use SetBased\Audit\ColumnTypes;
7
use SetBased\Audit\MySql\DataLayer;
8
use SetBased\Stratum\MySql\StaticDataLayer;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
11
/**
12
 * A helper class for creating printing Tables.
13
 */
14
class TableHelper
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * Array with rows for table.
19
   *
20
   * @var \array[]
21
   */
22
  private $rows = [];
23
24
  /**
25
   * Audit columns from config file.
26
   *
27
   * @var array
28
   */
29
  private $auditColumns;
30
31
  /**
32
   * Table options from audit schema.
33
   *
34
   * @var array
35
   */
36
  private $auditTableOptions;
37
38
  /**
39
   * Table options from data schema.
40
   *
41
   * @var array
42
   */
43
  private $dataTableOptions;
44
45
  //--------------------------------------------------------------------------------------------------------------------
46
  /**
47
   * Object constructor.
48
   *
49
   * @param string  $dataSchema      Data schema name.
50
   * @param string  $auditSchema     Audit schema name.
51
   * @param string  $tableName       The table name.
52
   * @param array[] $theAuditColumns Audit columns from config file.
53
   */
54
  public function __construct($dataSchema, $auditSchema, $tableName, $theAuditColumns)
55
  {
56
    $this->auditColumns      = $theAuditColumns;
57
    $this->auditTableOptions = DataLayer::getTableOptions($auditSchema, $tableName);
58
    $this->dataTableOptions  = DataLayer::getTableOptions($dataSchema, $tableName);
59
  }
60
61
  //--------------------------------------------------------------------------------------------------------------------
62
  /**
63
   * Append row with table option.
64
   *
65
   * @param string $theOption
66
   */
67
  public function appendTableOption($theOption)
68
  {
69
    $tableRow               = ['column_name'        => $theOption,
70
                               'data_column_type'   => $this->dataTableOptions[$theOption],
71
                               'audit_column_type'  => $this->auditTableOptions[$theOption],
72
                               'config_column_type' => null];
73
    $this->rows[$theOption] = RowHelper::createTableRow($tableRow);
74
  }
75
76
  //--------------------------------------------------------------------------------------------------------------------
77
  /**
78
   * Appends rows.
79
   *
80
   * @param \array[] $theRows
81
   */
82
  public function appendRows($theRows)
83
  {
84
    /** @var ColumnTypes $row */
85
    foreach ($theRows as $row)
86
    {
87
      RowHelper::appendRow($this->rows, $row);
88
    }
89
    $this->rows[] = new TableSeparator();
90
    $this->appendTableOption('engine');
91
    $this->appendTableOption('character_set_name');
92
  }
93
94
  //--------------------------------------------------------------------------------------------------------------------
95
  /**
96
   * Add highlighting to columns.
97
   *
98
   * @return array[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
99
   */
100
  public function addHighlighting()
101
  {
102
    $styledColumns = [];
103
    foreach ($this->rows as $column)
104
    {
105
      $styledColumn = $column;
106
      if (is_array($column))
107
      {
108
        // Highlighting for data table column types and audit.
109
        if (!empty($column['data_table_type']))
110
        {
111
          if (isset($column['data_table_type']) && !isset($column['audit_table_type']))
112
          {
113
            $styledColumn['column_name']     = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
114
            $styledColumn['data_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']);
115
          }
116 View Code Duplication
          else if (!isset($column['data_table_type']) && isset($column['audit_table_type']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
          {
118
            $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']);
119
          }
120
          else if (strcmp($column['data_table_type'], $column['audit_table_type']))
121
          {
122
            $styledColumn['column_name']      = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
123
            $styledColumn['data_table_type']  = sprintf('<mm_type>%s</>', $styledColumn['data_table_type']);
124
            $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $styledColumn['audit_table_type']);
125
          }
126
        }
127
        else
128
        {
129
          // Highlighting for audit table column types and audit_columns in config file.
130
          $searchColumn = StaticDataLayer::searchInRowSet('column_name', $styledColumn['column_name'], $this->auditColumns);
131
          if (isset($searchColumn))
132
          {
133
            $configType = $this->auditColumns[$searchColumn]['column_type'];
134
            if (isset($configType) && !isset($column['audit_table_type']))
135
            {
136
              $styledColumn['column_name'] = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
137
              $styledColumn['config_type'] = sprintf('<mm_type>%s</>', $styledColumn['config_type']);
138
            }
139 View Code Duplication
            else if (!isset($configType) && isset($column['audit_table_type']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
            {
141
              $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']);
142
            }
143
            else if (strcmp($configType, $column['audit_table_type']))
144
            {
145
              $styledColumn['column_name']      = sprintf('<mm_column>%s</>', $styledColumn['column_name']);
146
              $styledColumn['audit_table_type'] = sprintf('<mm_type>%s</>', $column['audit_table_type']);
147
              $styledColumn['config_type']      = sprintf('<mm_type>%s</>', $styledColumn['config_type']);
148
            }
149
          }
150
        }
151
      }
152
      $styledColumns[] = $styledColumn;
153
    }
154
155
    $this->rows = $styledColumns;
156
  }
157
158
  //--------------------------------------------------------------------------------------------------------------------
159
  /**
160
   * Get rows.
161
   *
162
   * @return \array[]
163
   */
164
  public function getRows()
165
  {
166
    return $this->rows;
167
  }
168
169
  //--------------------------------------------------------------------------------------------------------------------
170
}
171
172
//----------------------------------------------------------------------------------------------------------------------
173