Completed
Push — master ( c95337...5e0152 )
by P.R.
04:34
created

CreateAuditTable::getColumnDefinitions()   C

Complexity

Conditions 8
Paths 49

Size

Total Lines 51
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.006

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 51
ccs 21
cts 22
cp 0.9545
rs 6.5978
cc 8
eloc 22
nc 49
nop 0
crap 8.006

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 13
  public function __construct($dataSchemaName,
55
                              $auditSchemaName,
56
                              $tableName,
57
                              $columns)
58
  {
59 13
    $this->dataSchemaName  = $dataSchemaName;
60 13
    $this->auditSchemaName = $auditSchemaName;
61 13
    $this->tableName       = $tableName;
62 13
    $this->columns         = $columns;
63 13
  }
64
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * Returns a SQL statement for creating the audit table.
68
   *
69
   * @return string
70
   */
71 13
  public function buildStatement()
72
  {
73 13
    $code = new MySqlCompoundSyntaxCodeStore();
74
75 13
    $code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName));
76
77
    // Create SQL for columns.
78 13
    $code->append('(');
79 13
    $code->append($this->getColumnDefinitions());
80
81
    // Create SQL for table options.
82 13
    $tableOptions = AuditDataLayer::getTableOptions($this->dataSchemaName, $this->tableName);
83 13
    $code->append(sprintf(') engine=%s character set=%s collate=%s',
84 13
                          $tableOptions['engine'],
85 13
                          $tableOptions['character_set_name'],
86 13
                          $tableOptions['table_collation']));
87
88 13
    return $code->getCode();
89
  }
90
91
  //--------------------------------------------------------------------------------------------------------------------
92
  /**
93
   * Returns an array with SQL code for column definitions.
94
   *
95
   * @return string[]
96
   */
97 13
  private function getColumnDefinitions()
98
  {
99 13
    $lines = [];
100
101
    // Base format on column with longest name.
102 13
    $format = $this->getFormat();
103
104 13
    $columns = $this->columns->getColumns();
105 13
    foreach ($columns as $column)
106
    {
107 13
      $line = sprintf($format, '`'.$column->getProperty('column_name').'`', $column->getProperty('column_type'));
108
109
      // Timestamps require special settings for null values.
110 13
      if ($column->getProperty('column_type')=='timestamp')
111
      {
112 2
        $line .= ' null';
113
114 2
        if ($column->getProperty('is_nullable')=='YES')
115
        {
116
          $line .= ' default null';
117
        }
118
      }
119
120
      // Set character set and collation.
121 13
      if ($column->getProperty('character_set_name'))
122
      {
123 7
        $line .= ' character set ';
124 7
        $line .= $column->getProperty('character_set_name');
125
      }
126 13
      if ($column->getProperty('collation_name'))
127
      {
128 7
        $line .= ' collate ';
129 7
        $line .= $column->getProperty('collation_name');
130
      }
131
132
      // Set nullable.
133 13
      if ($column->getProperty('is_nullable')=='NO')
134
      {
135 6
        $line .= ' not null';
136
      }
137
138 13
      if (end($columns)!==$column)
139
      {
140 13
        $line .= ',';
141
      }
142
143 13
      $lines[] = $line;
144
    }
145
146 13
    return $lines;
147
  }
148
149
  //--------------------------------------------------------------------------------------------------------------------
150
  /**
151
   * Returns the format specifier for printing column names and column types
152
   *
153
   * @return string
154
   */
155 13
  private function getFormat()
156
  {
157 13
    $width = 0;
158 13
    foreach ($this->columns->getColumns() as $column)
159
    {
160 13
      $width = max($width, mb_strlen($column->getProperty('column_name')));
161
    }
162
163 13
    return sprintf('  %%-%ds %%s', $width + 2);
164
  }
165
166
  //--------------------------------------------------------------------------------------------------------------------
167
}
168
169
//----------------------------------------------------------------------------------------------------------------------
170