AddForeignKeysToTable::getItem()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
nc 16
nop 1
dl 0
loc 22
ccs 0
cts 22
cp 0
crap 30
rs 9.4555
c 1
b 0
f 0
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
        $value = $foreignKey['field'];
18
        if (!empty($foreignKey['name'])) {
19
            $value .= "', '".$foreignKey['name'];
20
        }
21
        $output = sprintf(
22
            "\$table->foreign('%s')->references('%s')->on('%s')",
23
            $value,
24
            $foreignKey['references'],
25
            $foreignKey['on']
26
        );
27
        if ($foreignKey['onUpdate']) {
28
            $output .= sprintf("->onUpdate('%s')", $foreignKey['onUpdate']);
29
        }
30
        if ($foreignKey['onDelete']) {
31
            $output .= sprintf("->onDelete('%s')", $foreignKey['onDelete']);
32
        }
33
        if (isset($foreignKey['decorators'])) {
34
            $output .= $this->addDecorators($foreignKey['decorators']);
35
        }
36
        return $output.';';
37
    }
38
}
39