Completed
Pull Request — master (#43)
by Dima
09:15
created

DiffTableRowHelper::checkOptions()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 8.8571
cc 5
eloc 7
nc 5
nop 1
crap 30
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\MySql\Helper;
4
5
use SetBased\Audit\MySql\Metadata\MultiSourceColumnMetadata;
6
7
//----------------------------------------------------------------------------------------------------------------------
8
/**
9
 * A helper class for DiffTable rows.
10
 */
11
class DiffTableRowHelper
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * Append a row.
16
   *
17
   * @param \array[]                  $theExistRows Exist rows array for appending.
18
   * @param MultiSourceColumnMetadata $rowMetadata  Row for append.
19
   * @param string                    $columnName   The columns name.
20
   */
21
  public static function appendRow(&$theExistRows, $rowMetadata, $columnName)
22
  {
23
    $theExistRows[] = self::createTableRow($rowMetadata, $columnName);
24
    if (self::checkOptions($rowMetadata))
25
    {
26
      $theRow = self::createColumnOptionsRow($rowMetadata);
27
28
      $theExistRows[] = $theRow;
29
    }
30
  }
31
32
  //--------------------------------------------------------------------------------------------------------------------
33
  /**
34
   * Create table row.
35
   *
36
   * @param MultiSourceColumnMetadata $rowMetadata Data for table row.
37
   *
38
   * @return array<string,null|string>
39
   */
40
  public static function createColumnOptionsRow($rowMetadata)
41
  {
42
    $columnProperties = $rowMetadata->getProperties();
43
    $dataMetadata     = isset($columnProperties['data']) ? $columnProperties['data']->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties['data'] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
44
    $auditMetadata    = isset($columnProperties['audit']) ? $columnProperties['audit']->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties['audit'] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
45
    $configMetadata   = isset($columnProperties['config']) ? $columnProperties['config']->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties['config'] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
46
47
    $dataCharsetName   = isset($dataMetadata['character_set_name']) ? $dataMetadata['character_set_name'] : null;
48
    $dataCollationName = isset($dataMetadata['collation_name']) ? $dataMetadata['collation_name'] : null;
49
50
    $auditCharsetName   = isset($auditMetadata['character_set_name']) ? $auditMetadata['character_set_name'] : null;
51
    $auditCollationName = isset($auditMetadata['collation_name']) ? $auditMetadata['collation_name'] : null;
52
53
    $configCharsetName   = isset($configMetadata['character_set_name']) ? $configMetadata['character_set_name'] : null;
54
    $configCollationName = isset($configMetadata['collation_name']) ? $configMetadata['collation_name'] : null;
55
56
    return ['column_name' => null,
57
            'data'        => self::styledOptionsRow($dataCharsetName, $dataCollationName),
58
            'audit'       => self::styledOptionsRow($auditCharsetName, $auditCollationName),
59
            'config'      => self::styledOptionsRow($configCharsetName, $configCollationName)];
60
  }
61
62
  //--------------------------------------------------------------------------------------------------------------------
63
  /**
64
   * Create table row.
65
   *
66
   * @param MultiSourceColumnMetadata $rowMetadata Data for table row.
67
   * @param string                    $columnName  The columns name.
68
   *
69
   * @return array<string,null|string>
70
   */
71
  public static function createTableRow($rowMetadata, $columnName)
72
  {
73
    $columnProperties = $rowMetadata->getProperties();
74
    $dataMetadata     = isset($columnProperties['data']) ? $columnProperties['data']->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties['data'] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
75
    $auditMetadata    = isset($columnProperties['audit']) ? $columnProperties['audit']->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties['audit'] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
76
    $configMetadata   = isset($columnProperties['config']) ? $columnProperties['config']->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties['config'] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
77
78
    return ['column_name' => $columnName,
79
            'data'        => isset($dataMetadata['column_type']) ? $dataMetadata['column_type'] : null,
80
            'audit'       => isset($auditMetadata['column_type']) ? $auditMetadata['column_type'] : null,
81
            'config'      => isset($configMetadata['column_type']) ? $configMetadata['column_type'] : null];
82
  }
83
84
  //--------------------------------------------------------------------------------------------------------------------
85
  /**
86
   * Create table row.
87
   *
88
   * @param $theCharacterSetName
89
   * @param $theCollationName
90
   *
91
   * @return string
92
   */
93
  public static function styledOptionsRow($theCharacterSetName, $theCollationName)
94
  {
95
    $charsetName   = isset($theCharacterSetName) ? '['.$theCharacterSetName.']' : null;
96
    $collationName = isset($theCollationName) ? '['.$theCollationName.']' : null;
97
98
    return trim(sprintf('%s %s', $charsetName, $collationName));
99
  }
100
101
  //--------------------------------------------------------------------------------------------------------------------
102
  /**
103
   * Check isset options(collation, character set name) from row.
104
   *
105
   * @param MultiSourceColumnMetadata $rowMetadata Row for append.
106
   *
107
   * @return bool
108
   */
109
  private static function checkOptions($rowMetadata)
110
  {
111
    $columnProperties = $rowMetadata->getProperties();
112
    foreach ($rowMetadata->getProperties() as $sourceName => $metadata)
113
    {
114
      $data = isset($columnProperties[$sourceName]) ? $columnProperties[$sourceName]->getProperties() : null;
0 ignored issues
show
Bug introduced by
The method getProperties cannot be called on $columnProperties[$sourceName] (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
115
      if (isset($data['character_set_name']) || isset($data['collation_name']))
116
      {
117
        return true;
118
      }
119
    }
120
121
    return false;
122
  }
123
124
  //--------------------------------------------------------------------------------------------------------------------
125
}
126
127
//----------------------------------------------------------------------------------------------------------------------
128