Completed
Pull Request — master (#55)
by Dima
03:18
created

DiffTableRowHelper   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

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 52
cts 52
cp 1
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 4
  public static function appendRow(&$theExistRows, $rowMetadata, $columnName)
23
  {
24 4
    $theExistRows[] = self::createTableRow($rowMetadata, $columnName);
25 4
    if (self::checkOptions($rowMetadata))
26 4
    {
27 2
      $theRow = self::createColumnOptionsRow($rowMetadata);
28
29 2
      $theExistRows[] = $theRow;
30 2
    }
31 4
  }
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 2
  public static function createColumnOptionsRow($rowMetadata)
42
  {
43 2
    $columnProperties = $rowMetadata->getProperties();
44
    /** @var ColumnMetadata $data */
45 2
    $data = isset($columnProperties['data']) ? $columnProperties['data'] : null;
46
    /** @var ColumnMetadata $audit */
47 2
    $audit = isset($columnProperties['audit']) ? $columnProperties['audit'] : null;
48
    /** @var ColumnMetadata $config */
49 2
    $config = isset($columnProperties['config']) ? $columnProperties['config'] : null;
50
51 2
    $dataMetadata   = isset($data) ? $data->getProperties() : null;
52 2
    $auditMetadata  = isset($audit) ? $audit->getProperties() : null;
53 2
    $configMetadata = isset($config) ? $config->getProperties() : null;
54
55 2
    $dataCharsetName   = isset($dataMetadata['character_set_name']) ? $dataMetadata['character_set_name'] : null;
56 2
    $dataCollationName = isset($dataMetadata['collation_name']) ? $dataMetadata['collation_name'] : null;
57
58 2
    $auditCharsetName   = isset($auditMetadata['character_set_name']) ? $auditMetadata['character_set_name'] : null;
59 2
    $auditCollationName = isset($auditMetadata['collation_name']) ? $auditMetadata['collation_name'] : null;
60
61 2
    $configCharsetName   = isset($configMetadata['character_set_name']) ? $configMetadata['character_set_name'] : null;
62 2
    $configCollationName = isset($configMetadata['collation_name']) ? $configMetadata['collation_name'] : null;
63
64 2
    return ['column_name' => null,
65 2
            'data'        => self::styledOptionsRow($dataCharsetName, $dataCollationName),
66 2
            'audit'       => self::styledOptionsRow($auditCharsetName, $auditCollationName),
67 2
            '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 4
  public static function createTableRow($rowMetadata, $columnName)
80
  {
81 4
    $columnProperties = $rowMetadata->getProperties();
82
    /** @var ColumnMetadata $data */
83 4
    $data = isset($columnProperties['data']) ? $columnProperties['data'] : null;
84
    /** @var ColumnMetadata $audit */
85 4
    $audit = isset($columnProperties['audit']) ? $columnProperties['audit'] : null;
86
    /** @var ColumnMetadata $config */
87 4
    $config = isset($columnProperties['config']) ? $columnProperties['config'] : null;
88
89 4
    $dataMetadata   = isset($data) ? $data->getProperties() : null;
90 4
    $auditMetadata  = isset($audit) ? $audit->getProperties() : null;
91 4
    $configMetadata = isset($config) ? $config->getProperties() : null;
92
93 4
    return ['column_name' => $columnName,
94 4
            'data'        => isset($dataMetadata['column_type']) ? $dataMetadata['column_type'] : null,
95 4
            'audit'       => isset($auditMetadata['column_type']) ? $auditMetadata['column_type'] : null,
96 4
            '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 2
  public static function styledOptionsRow($theCharacterSetName, $theCollationName)
109
  {
110 2
    $charsetName   = isset($theCharacterSetName) ? '['.$theCharacterSetName.']' : null;
111 2
    $collationName = isset($theCollationName) ? '['.$theCollationName.']' : null;
112
113 2
    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 4
  private static function checkOptions($rowMetadata)
125
  {
126 4
    $columnProperties = $rowMetadata->getProperties();
127 4
    foreach ($rowMetadata->getProperties() as $sourceName => $metadata)
128
    {
129
      /** @var ColumnMetadata $source */
130 4
      $source = isset($columnProperties[$sourceName]) ? $columnProperties[$sourceName] : null;
131 4
      $data   = isset($source) ? $source->getProperties() : null;
132 4
      if (isset($data['character_set_name']) || isset($data['collation_name']))
133 4
      {
134 2
        return true;
135
      }
136 3
    }
137
138 2
    return false;
139
  }
140
141
  //--------------------------------------------------------------------------------------------------------------------
142
}
143
144
//----------------------------------------------------------------------------------------------------------------------
145