Passed
Push — 4.x ( b027a1...820cf2 )
by Kit Loong
62:09
created

Table::run()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 7
b 0
f 0
nc 6
nop 4
dl 0
loc 24
ccs 0
cts 11
cp 0
crap 20
rs 9.7998
1
<?php namespace Xethron\MigrationsGenerator\Syntax;
2
3
use Illuminate\Support\Facades\Config;
4
use KitLoong\MigrationsGenerator\Generators\Decorator;
5
use KitLoong\MigrationsGenerator\Generators\Platform;
6
use KitLoong\MigrationsGenerator\MigrationsGeneratorSetting;
7
use Way\Generators\Compilers\TemplateCompiler;
8
use Way\Generators\Syntax\Table as WayTable;
9
10
/**
11
 * Class Table
12
 * @package Xethron\MigrationsGenerator\Syntax
13
 */
14
abstract class Table extends WayTable
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $table;
20
21
    public function __construct(TemplateCompiler $compiler, Decorator $decorator)
22
    {
23
        parent::__construct($compiler, $decorator);
24
    }
25
26
    public function run(array $fields, string $table, string $connection, $method = 'table'): string
27
    {
28
        $table = $this->decorator->tableWithoutPrefix($table);
29
        $this->table = $table;
30
        if ($connection !== Config::get('database.default')) {
31
            $method = 'connection(\''.$connection.'\')->'.$method;
32
        }
33
34
        $compiled = $this->compiler->compile($this->getTemplate($method), ['table' => $table, 'method' => $method]);
35
36
        $content = $this->getItems($fields);
37
38
        if ($method === 'create') {
39
            $tableCollation = $this->getTableCollation($table);
40
            if (!empty($tableCollation)) {
41
                $content = array_merge(
42
                    $tableCollation,
43
                    [''], // New line
44
                    $content
45
                );
46
            }
47
        }
48
49
        return $this->replaceFieldsWith($content, $compiled);
50
    }
51
52
    /**
53
     * Return string for adding all foreign keys
54
     *
55
     * @param  array  $items
56
     * @return array
57
     */
58
    protected function getItems(array $items): array
59
    {
60
        $result = [];
61
        foreach ($items as $item) {
62
            $result[] = $this->getItem($item);
63
        }
64
        return $result;
65
    }
66
67
    /**
68
     * Get table collation migration lines if not equal to DB collation.
69
     *
70
     * @param  string  $tableName
71
     * @return array|string[]
72
     */
73
    protected function getTableCollation(string $tableName): array
74
    {
75
        $setting = app(MigrationsGeneratorSetting::class);
76
        if ($setting->getPlatform() === Platform::MYSQL) {
77
            if ($setting->isUseDBCollation()) {
78
                $tableCollation = $setting->getSchema()->listTableDetails($tableName)->getOptions()['collation'];
79
                $tableCharset = explode('_', $tableCollation)[0];
80
                return [
81
                    '$table->charset = \''.$tableCharset.'\';',
82
                    '$table->collation = \''.$tableCollation.'\';',
83
                ];
84
            }
85
        }
86
        return [];
87
    }
88
89
    /**
90
     * @param  array  $item
91
     * @return string
92
     */
93
    abstract protected function getItem(array $item): string;
94
95
    /**
96
     * @param  string[]  $decorators
97
     * @return string
98
     */
99
    protected function addDecorators(array $decorators): string
100
    {
101
        $output = '';
102
        foreach ($decorators as $decorator) {
103
            $output .= sprintf("->%s", $decorator);
104
            // Do we need to tack on the parentheses?
105
            if (strpos($decorator, '(') === false) {
106
                $output .= '()';
107
            }
108
        }
109
        return $output;
110
    }
111
}
112