Completed
Push — master ( 8e94f8...744b49 )
by P.R.
04:26
created

CreateAuditTable::getColumnDefinitions()   C

Complexity

Conditions 8
Paths 49

Size

Total Lines 51
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 8.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 33
cts 35
cp 0.9429
rs 6.5978
cc 8
eloc 22
nc 49
nop 0
crap 8.0119

How to fix   Long Method   

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\Sql;
4
5
use SetBased\Audit\MySql\AuditDataLayer;
6
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata;
7
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore;
8
9
//----------------------------------------------------------------------------------------------------------------------
10
/**
11
 * Class for creating SQL statements for creating audit tables.
12
 */
13
class CreateAuditTable
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * The name of the audit schema.
18
   *
19
   * @var string
20
   */
21
  private $auditSchemaName;
22
23
  /**
24
   * The name of the table.
25
   *
26
   * @var TableColumnsMetadata
27
   */
28
  private $columns;
29
30
  /**
31
   * The name of the data schema.
32
   *
33
   * @var string
34
   */
35
  private $dataSchemaName;
36
37
  /**
38
   * The name of the table.
39
   *
40
   * @var string
41
   */
42
  private $tableName;
43
44
  //--------------------------------------------------------------------------------------------------------------------
45
  /**
46
   * Object constructor.
47
   *
48
   * @param string               $dataSchemaName  The name of the data schema.
49
   * @param string               $auditSchemaName The name of the audit schema.
50
   * @param string               $tableName       The name of the table.
51
   * @param TableColumnsMetadata $columns         The metadata of the columns of the audit table (i.e. the audit
52
   *                                              columns and columns of the data table).
53
   */
54 5
  public function __construct($dataSchemaName,
55
                              $auditSchemaName,
56
                              $tableName,
57
                              $columns)
58
  {
59 5
    $this->dataSchemaName  = $dataSchemaName;
60 5
    $this->auditSchemaName = $auditSchemaName;
61 5
    $this->tableName       = $tableName;
62 5
    $this->columns         = $columns;
63 5
  }
64
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * Returns a SQL statement for creating the audit table.
68
   *
69
   * @return string
70
   */
71 5
  public function buildStatement()
72
  {
73 5
    $code = new MySqlCompoundSyntaxCodeStore();
74
75 5
    $code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName));
76
77
    // Create SQL for columns.
78 5
    $code->append('(');
79 5
    $code->append($this->getColumnDefinitions());
80
81
    // Create SQL for table options.
82 5
    $tableOptions = AuditDataLayer::getTableOptions($this->dataSchemaName, $this->tableName);
83 5
    $code->append(sprintf(') engine=%s character set=%s collate=%s',
84 5
                          $tableOptions['engine'],
85 5
                          $tableOptions['character_set_name'],
86 5
                          $tableOptions['table_collation']));
87
88 5
    return $code->getCode();
89
  }
90
91
  //--------------------------------------------------------------------------------------------------------------------
92
  /**
93
   * Retuns an array with SQL code for column definitions.
94
   *
95
   * @return string[]
96
   */
97 5
  private function getColumnDefinitions()
98
  {
99 5
    $lines = [];
100
101
    // Base format on column with longest name.
102 5
    $format = $this->getFormat();
103
104 5
    $columns = $this->columns->getColumns();
105 5
    foreach ($columns as $column)
106
    {
107 5
      $line = sprintf($format, '`'.$column->getProperty('column_name').'`', $column->getProperty('column_type'));
108
109
      // Timestamps require special settings for null values.
110 5
      if ($column->getProperty('column_type')=='timestamp')
111 5
      {
112 2
        $line .= ' null';
113
114 2
        if ($column->getProperty('is_nullable')=='YES')
115 2
        {
116
          $line .= ' default null';
117
        }
118 2
      }
119
120
      // Set character set and collation.
121 5
      if ($column->getProperty('character_set_name'))
122 5
      {
123 5
        $line .= ' character set ';
124 5
        $line .= $column->getProperty('character_set_name');
125 5
      }
126 5
      if ($column->getProperty('collation_name'))
127 5
      {
128 5
        $line .= ' collate ';
129 5
        $line .= $column->getProperty('collation_name');
130 5
      }
131
132
      // Set nullable.
133 5
      if ($column->getProperty('is_nullable')=='NO')
134 5
      {
135 5
        $line .= ' not null';
136 5
      }
137
138 5
      if (end($columns)!==$column)
139 5
      {
140 5
        $line .= ',';
141 5
      }
142
143 5
      $lines[] = $line;
144 5
    }
145
146 5
    return $lines;
147
  }
148
149
  //--------------------------------------------------------------------------------------------------------------------
150
  /**
151
   * Returns the format specifier for printing column names and column types
152
   *
153
   * @return string
154
   */
155 5
  private function getFormat()
156
  {
157 5
    $width = 0;
158 5
    foreach ($this->columns->getColumns() as $column)
159
    {
160 5
      $width = max($width, mb_strlen($column->getProperty('column_name')));
161 5
    }
162
163 5
    return sprintf('  %%-%ds %%s', $width + 2);
164
  }
165
166
  //--------------------------------------------------------------------------------------------------------------------
167
}
168
169
//----------------------------------------------------------------------------------------------------------------------
170