Completed
Pull Request — master (#28)
by
unknown
02:53
created

TableHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 9
c 4
b 3
f 0
lcom 1
cbo 2
dl 0
loc 111
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B appendTableOption() 0 19 5
A appendRows() 0 10 2
A getRows() 0 4 1
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
//----------------------------------------------------------------------------------------------------------------------
6
use SetBased\Audit\MySql\DataLayer;
7
use Symfony\Component\Console\Helper\TableSeparator;
8
9
/**
10
 * A helper class for creating printing Tables.
11
 */
12
class TableHelper
13
{
14
  //--------------------------------------------------------------------------------------------------------------------
15
  /**
16
   * Array with rows for table.
17
   *
18
   * @var \array[]
19
   */
20
  private $rows = [];
21
22
  /**
23
   * Table options from audit schema.
24
   *
25
   * @var array
26
   */
27
  private $auditTableOptions;
28
29
  /**
30
   * Table options from data schema.
31
   *
32
   * @var array
33
   */
34
  private $dataTableOptions;
35
36
  /**
37
   * Full option.
38
   *
39
   * @var bool
40
   */
41
  private $fullOption;
42
43
  /**
44
   * Check existing separator.
45
   *
46
   * @var bool
47
   */
48
  private $existSeparator = false;
49
50
  //--------------------------------------------------------------------------------------------------------------------
51
  /**
52
   * Object constructor.
53
   *
54
   * @param string $dataSchema  Data schema name.
55
   * @param string $auditSchema Audit schema name.
56
   * @param string $tableName   The table name.
57
   * @param bool   $fullOption  If set append table options to rows.
58
   */
59
  public function __construct($dataSchema, $auditSchema, $tableName, $fullOption)
60
  {
61
    $this->fullOption        = $fullOption;
62
    $this->auditTableOptions = DataLayer::getTableOptions($auditSchema, $tableName);
63
    $this->dataTableOptions  = DataLayer::getTableOptions($dataSchema, $tableName);
64
  }
65
66
  //--------------------------------------------------------------------------------------------------------------------
67
  /**
68
   * Append row with table option.
69
   *
70
   * @param string      $theOption The option.
71
   * @param null|string $theName   Display name.
72
   */
73
  public function appendTableOption($theOption, $theName = null)
74
  {
75
    if ($this->dataTableOptions[$theOption]!=$this->auditTableOptions[$theOption] || $this->fullOption)
76
    {
77
      if (!$this->existSeparator)
78
      {
79
        $this->rows[]         = new TableSeparator();
80
        $this->existSeparator = true;
81
      }
82
      if ($theName===null)
83
      {
84
        $theName = $theOption;
85
      }
86
      $this->rows[$theOption] = ['column_name'      => $theName,
87
                                 'data_table_type'  => $this->dataTableOptions[$theOption],
88
                                 'audit_table_type' => $this->auditTableOptions[$theOption],
89
                                 'config_type'      => null];
90
    }
91
  }
92
93
  //--------------------------------------------------------------------------------------------------------------------
94
  /**
95
   * Appends rows.
96
   *
97
   * @param \array[] $theRows Rows array.
98
   */
99
  public function appendRows($theRows)
100
  {
101
    foreach ($theRows as $row)
102
    {
103
      $this->rows[] = $row;
104
    }
105
    $this->appendTableOption('engine');
106
    $this->appendTableOption('character_set_name', 'character set');
107
    $this->appendTableOption('table_collation', 'collation');
108
  }
109
110
  //--------------------------------------------------------------------------------------------------------------------
111
  /**
112
   * Get rows.
113
   *
114
   * @return \array[]
115
   */
116
  public function getRows()
117
  {
118
    return $this->rows;
119
  }
120
121
  //--------------------------------------------------------------------------------------------------------------------
122
}
123
124
//----------------------------------------------------------------------------------------------------------------------
125