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

AlterAuditTableAddColumns   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 72
ccs 16
cts 18
cp 0.8889
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildStatement() 0 26 4
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\Metadata\ColumnMetadata;
8
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore;
9
10
/**
11
 * Class for creating SQL statements for adding new columns to an audit table.
12
 */
13
class AlterAuditTableAddColumns
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * The name of the audit schema.
18
   *
19
   * @var string
20
   */
21
  private $auditSchemaName;
22
23
  /**
24
   * The array of new columns for adding to table.
25
   *
26
   * @var TableColumnsMetadata
27
   */
28
  private $columns;
29
30
  /**
31
   * The name of the audit table.
32
   *
33
   * @var string
34
   */
35
  private $tableName;
36
37
  //--------------------------------------------------------------------------------------------------------------------
38
  /**
39
   * Object constructor.
40
   *
41
   * @param string               $auditSchemaName The name of the audit schema.
42
   * @param string               $tableName       The name of the table.
43
   * @param TableColumnsMetadata $columns         The metadata of the new columns of the audit table (i.e. the audit
44
   *                                              columns and columns of the data table).
45
   */
46 3
  public function __construct(string $auditSchemaName, string $tableName, TableColumnsMetadata $columns)
47
  {
48 3
    $this->auditSchemaName = $auditSchemaName;
49 3
    $this->tableName       = $tableName;
50 3
    $this->columns         = $columns;
51 3
  }
52
53
  //--------------------------------------------------------------------------------------------------------------------
54
  /**
55
   * Returns a SQL statement for adding new columns to the audit table.
56
   *
57
   * @return string
58
   */
59 3
  public function buildStatement(): string
60
  {
61 3
    $code = new MySqlCompoundSyntaxCodeStore();
62
63 3
    $code->append(sprintf('alter table `%s`.`%s`', $this->auditSchemaName, $this->tableName));
64
    /** @var ColumnMetadata $column */
65 3
    foreach ($this->columns->getColumns() as $column)
66
    {
67 3
      $code->append(sprintf('  add `%s` %s', $column->getName(), $column->getProperty('column_type')), false);
68 3
      $after = $column->getProperty('after');
69 3
      if (isset($after))
70
      {
71 3
        $code->appendToLastLine(sprintf(' after `%s`', $after));
72
      }
73
      else
74
      {
75
        $code->appendToLastLine(' first');
76
      }
77 3
      $columns = $this->columns->getColumns();
78 3
      if (end($columns)!==$column)
79
      {
80
        $code->appendToLastLine(',');
81
      }
82
    }
83
84 3
    return $code->getCode();
85
  }
86
87
  //--------------------------------------------------------------------------------------------------------------------
88
}
89
90
//----------------------------------------------------------------------------------------------------------------------
91