AddForeignKeysToTable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 30
ccs 0
cts 22
cp 0
rs 10
c 2
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 22 5
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