Completed
Push — 4.x ( 0e04b4...1a8126 )
by Kit Loong
15s queued 13s
created

AddForeignKeysToTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 21
c 3
b 0
f 0
dl 0
loc 39
ccs 0
cts 23
cp 0
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B getItem() 0 31 6
1
<?php namespace Xethron\MigrationsGenerator\Syntax;
2
3
/**
4
 * Class AddForeignKeysToTable
5
 * @package Xethron\MigrationsGenerator\Syntax
6
 */
7
class AddForeignKeysToTable extends Table
8
{
9
    /**
10
     * Return string for adding a foreign key
11
     *
12
     * @param  array  $foreignKey
13
     * @return string
14
     */
15
    protected function getItem(array $foreignKey): string
16
    {
17
        // Check for multiple columns
18
        if (count($foreignKey['field']) > 1) {
19
            $value = "['" . implode("', '", $foreignKey['field']) . "']";
20
            $references = "['" . implode("', '", $foreignKey['references']) . "']";
21
        } else {
22
            $value = "'" . $foreignKey['field'][0] . "'";
23
            $references = "'" . $foreignKey['references'][0] . "'";
24
        }
25
26
        if (!empty($foreignKey['name'])) {
27
            $value .= ", '" . $foreignKey['name'] . "'";
28
        }
29
30
        $output = sprintf(
31
            "\$table->foreign(%s)->references(%s)->on('%s')",
32
            $value,
33
            $references,
34
            $foreignKey['on']
35
        );
36
        if ($foreignKey['onUpdate']) {
37
            $output .= sprintf("->onUpdate('%s')", $foreignKey['onUpdate']);
38
        }
39
        if ($foreignKey['onDelete']) {
40
            $output .= sprintf("->onDelete('%s')", $foreignKey['onDelete']);
41
        }
42
        if (isset($foreignKey['decorators'])) {
43
            $output .= $this->addDecorators($foreignKey['decorators']);
44
        }
45
        return $output.';';
46
    }
47
}
48