Completed
Push — master ( c2bc7a...ffd46e )
by P.R.
56s
created

DiffTableRowHelper   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 34
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 131
ccs 0
cts 52
cp 0
rs 9.2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A appendRow() 0 10 2
F createColumnOptionsRow() 0 28 13
C createTableRow() 0 19 10
A styledOptionsRow() 0 7 3
B checkOptions() 0 16 6
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
use SetBased\Audit\MySql\Metadata\ColumnMetadata;
6
use SetBased\Audit\MySql\Metadata\MultiSourceColumnMetadata;
7
8
//----------------------------------------------------------------------------------------------------------------------
9
/**
10
 * A helper class for DiffTable rows.
11
 */
12
class DiffTableRowHelper
13
{
14
  //--------------------------------------------------------------------------------------------------------------------
15
  /**
16
   * Append a row to existing rows array.
17
   *
18
   * @param \array[]                  $theExistRows Exist rows array for appending.
19
   * @param MultiSourceColumnMetadata $rowMetadata  Row for append.
20
   * @param string                    $columnName   The columns name.
21
   */
22
  public static function appendRow(&$theExistRows, $rowMetadata, $columnName)
23
  {
24
    $theExistRows[] = self::createTableRow($rowMetadata, $columnName);
25
    if (self::checkOptions($rowMetadata))
26
    {
27
      $theRow = self::createColumnOptionsRow($rowMetadata);
28
29
      $theExistRows[] = $theRow;
30
    }
31
  }
32
33
  //--------------------------------------------------------------------------------------------------------------------
34
  /**
35
   * Create additional row for column with character set name and collation name.
36
   *
37
   * @param MultiSourceColumnMetadata $rowMetadata Data for table row.
38
   *
39
   * @return array<string,null|string>
40
   */
41
  public static function createColumnOptionsRow($rowMetadata)
42
  {
43
    $columnProperties = $rowMetadata->getProperties();
44
    /** @var ColumnMetadata $data */
45
    $data = isset($columnProperties['data']) ? $columnProperties['data'] : null;
46
    /** @var ColumnMetadata $audit */
47
    $audit = isset($columnProperties['audit']) ? $columnProperties['audit'] : null;
48
    /** @var ColumnMetadata $config */
49
    $config = isset($columnProperties['config']) ? $columnProperties['config'] : null;
50
51
    $dataMetadata   = isset($data) ? $data->getProperties() : null;
52
    $auditMetadata  = isset($audit) ? $audit->getProperties() : null;
53
    $configMetadata = isset($config) ? $config->getProperties() : null;
54
55
    $dataCharsetName   = isset($dataMetadata['character_set_name']) ? $dataMetadata['character_set_name'] : null;
56
    $dataCollationName = isset($dataMetadata['collation_name']) ? $dataMetadata['collation_name'] : null;
57
58
    $auditCharsetName   = isset($auditMetadata['character_set_name']) ? $auditMetadata['character_set_name'] : null;
59
    $auditCollationName = isset($auditMetadata['collation_name']) ? $auditMetadata['collation_name'] : null;
60
61
    $configCharsetName   = isset($configMetadata['character_set_name']) ? $configMetadata['character_set_name'] : null;
62
    $configCollationName = isset($configMetadata['collation_name']) ? $configMetadata['collation_name'] : null;
63
64
    return ['column_name' => null,
65
            'data'        => self::styledOptionsRow($dataCharsetName, $dataCollationName),
66
            'audit'       => self::styledOptionsRow($auditCharsetName, $auditCollationName),
67
            'config'      => self::styledOptionsRow($configCharsetName, $configCollationName)];
68
  }
69
70
  //--------------------------------------------------------------------------------------------------------------------
71
  /**
72
   * Create table row for columns with information from all schemas and config file.
73
   *
74
   * @param MultiSourceColumnMetadata $rowMetadata Data for table row.
75
   * @param string                    $columnName  The columns name.
76
   *
77
   * @return array<string,null|string>
78
   */
79
  public static function createTableRow($rowMetadata, $columnName)
80
  {
81
    $columnProperties = $rowMetadata->getProperties();
82
    /** @var ColumnMetadata $data */
83
    $data = isset($columnProperties['data']) ? $columnProperties['data'] : null;
84
    /** @var ColumnMetadata $audit */
85
    $audit = isset($columnProperties['audit']) ? $columnProperties['audit'] : null;
86
    /** @var ColumnMetadata $config */
87
    $config = isset($columnProperties['config']) ? $columnProperties['config'] : null;
88
89
    $dataMetadata   = isset($data) ? $data->getProperties() : null;
90
    $auditMetadata  = isset($audit) ? $audit->getProperties() : null;
91
    $configMetadata = isset($config) ? $config->getProperties() : null;
92
93
    return ['column_name' => $columnName,
94
            'data'        => isset($dataMetadata['column_type']) ? $dataMetadata['column_type'] : null,
95
            'audit'       => isset($auditMetadata['column_type']) ? $auditMetadata['column_type'] : null,
96
            'config'      => isset($configMetadata['column_type']) ? $configMetadata['column_type'] : null];
97
  }
98
99
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * Helper function for creating string with character set name and collation name.
102
   *
103
   * @param string $theCharacterSetName Character set name
104
   * @param string $theCollationName    Collation name
105
   *
106
   * @return string
107
   */
108
  public static function styledOptionsRow($theCharacterSetName, $theCollationName)
109
  {
110
    $charsetName   = isset($theCharacterSetName) ? '['.$theCharacterSetName.']' : null;
111
    $collationName = isset($theCollationName) ? '['.$theCollationName.']' : null;
112
113
    return trim(sprintf('%s %s', $charsetName, $collationName));
114
  }
115
116
  //--------------------------------------------------------------------------------------------------------------------
117
  /**
118
   * Check isset options(collation, character set name) from row.
119
   *
120
   * @param MultiSourceColumnMetadata $rowMetadata Row for append.
121
   *
122
   * @return bool
123
   */
124
  private static function checkOptions($rowMetadata)
125
  {
126
    $columnProperties = $rowMetadata->getProperties();
127
    foreach ($rowMetadata->getProperties() as $sourceName => $metadata)
128
    {
129
      /** @var ColumnMetadata $source */
130
      $source = isset($columnProperties[$sourceName]) ? $columnProperties[$sourceName] : null;
131
      $data   = isset($source) ? $source->getProperties() : null;
132
      if (isset($data['character_set_name']) || isset($data['collation_name']))
133
      {
134
        return true;
135
      }
136
    }
137
138
    return false;
139
  }
140
141
  //--------------------------------------------------------------------------------------------------------------------
142
}
143
144
//----------------------------------------------------------------------------------------------------------------------
145