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

AddForeignKeysToTable::getItem()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 2
b 0
f 0
nc 32
nop 1
dl 0
loc 31
ccs 0
cts 23
cp 0
crap 42
rs 8.9777
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