Passed
Push — master ( 9e86dc...6cbe7e )
by P.R.
06:56
created

CreateAuditTable::buildStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Audit\MySql\Sql;
5
6
use SetBased\Audit\Metadata\TableColumnsMetadata;
7
use SetBased\Audit\MySql\AuditDataLayer;
8
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore;
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 26
  public function __construct(string $dataSchemaName,
55
                              string $auditSchemaName,
56
                              string $tableName,
57
                              TableColumnsMetadata $columns)
58
  {
59 26
    $this->dataSchemaName  = $dataSchemaName;
60 26
    $this->auditSchemaName = $auditSchemaName;
61 26
    $this->tableName       = $tableName;
62 26
    $this->columns         = $columns;
63 26
  }
64
65
  //--------------------------------------------------------------------------------------------------------------------
66
  /**
67
   * Returns a SQL statement for creating the audit table.
68
   *
69
   * @return string
70
   */
71 26
  public function buildStatement(): string
72
  {
73 26
    $code = new MySqlCompoundSyntaxCodeStore();
74
75 26
    $code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName));
76
77
    // Create SQL for columns.
78 26
    $code->append('(');
79 26
    $code->append($this->getColumnDefinitions());
80
81
    // Create SQL for table options.
82 26
    $tableOptions = AuditDataLayer::$dl->getTableOptions($this->dataSchemaName, $this->tableName);
83 26
    $code->append(sprintf(') engine=%s character set=%s collate=%s',
84 26
                          $tableOptions['engine'],
85 26
                          $tableOptions['character_set_name'],
86 26
                          $tableOptions['table_collation']));
87
88 26
    return $code->getCode();
89
  }
90
91
  //--------------------------------------------------------------------------------------------------------------------
92
  /**
93
   * Returns an array with SQL code for column definitions.
94
   *
95
   * @return string[]
96
   */
97 26
  private function getColumnDefinitions(): array
98
  {
99 26
    $lines = [];
100
101 26
    $columns   = $this->columns->getColumns();
102 26
    $maxLength = $this->columns->getLongestColumnNameLength();
103 26
    foreach ($columns as $column)
104
    {
105 26
      $name   = $column->getName();
106 26
      $filler = str_repeat(' ', $maxLength - mb_strlen($name) + 1);
107
108 26
      $line = sprintf('  `%s`%s%s', $name, $filler, $column->getColumnAuditDefinition());
109
110 26
      if (end($columns)!==$column)
111
      {
112 26
        $line .= ',';
113
      }
114
115 26
      $lines[] = $line;
116
    }
117
118 26
    return $lines;
119
  }
120
121
  //--------------------------------------------------------------------------------------------------------------------
122
}
123
124
//----------------------------------------------------------------------------------------------------------------------
125