Completed
Pull Request — master (#52)
by Dima
03:17
created

DiffTableRowHelper::createTableRow()   C

Complexity

Conditions 10
Paths 512

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 0
cts 12
cp 0
rs 5.7204
cc 10
eloc 12
nc 512
nop 2
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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