Table   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 55
ccs 0
cts 28
cp 0
rs 10
c 9
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getItems() 0 7 2
A addDecorators() 0 11 3
A run() 0 10 2
1
<?php namespace Xethron\MigrationsGenerator\Syntax;
2
3
use Illuminate\Support\Facades\Config;
4
use Illuminate\Support\Facades\DB;
5
use Way\Generators\Syntax\Table as WayTable;
6
7
/**
8
 * Class Table
9
 * @package Xethron\MigrationsGenerator\Syntax
10
 */
11
abstract class Table extends WayTable
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $table;
17
18
    public function run(array $fields, string $table, string $connection, $method = 'table'): string
19
    {
20
        $table = $this->decorator->tableWithoutPrefix($table);
21
        $this->table = $table;
22
        if ($connection !== Config::get('database.default')) {
23
            $method = 'connection(\''.$connection.'\')->'.$method;
24
        }
25
26
        $compiled = $this->compiler->compile($this->getTemplate($method), ['table' => $table, 'method' => $method]);
27
        return $this->replaceFieldsWith($this->getItems($fields), $compiled);
28
    }
29
30
    /**
31
     * Return string for adding all foreign keys
32
     *
33
     * @param  array  $items
34
     * @return array
35
     */
36
    protected function getItems(array $items): array
37
    {
38
        $result = [];
39
        foreach ($items as $item) {
40
            $result[] = $this->getItem($item);
41
        }
42
        return $result;
43
    }
44
45
    /**
46
     * @param  array  $item
47
     * @return string
48
     */
49
    abstract protected function getItem(array $item): string;
50
51
    /**
52
     * @param  string[]  $decorators
53
     * @return string
54
     */
55
    protected function addDecorators(array $decorators): string
56
    {
57
        $output = '';
58
        foreach ($decorators as $decorator) {
59
            $output .= sprintf("->%s", $decorator);
60
            // Do we need to tack on the parentheses?
61
            if (strpos($decorator, '(') === false) {
62
                $output .= '()';
63
            }
64
        }
65
        return $output;
66
    }
67
}
68