Migration::generationField()   B
last analyzed

Complexity

Conditions 9
Paths 80

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 8.0555
cc 9
nc 80
nop 1
crap 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Builder\Components;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Builder\Package;
8
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
9
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface;
10
11
class Migration
12
{
13
    /**
14
     * @var Configuration
15
     */
16
    private Configuration $config;
17
18
    /**
19
     * @var Package
20
     */
21
    private Package $package;
22
23
    /**
24
     * FileGenerator constructor.
25
     * @param Configuration $config
26
     */
27 4
    public function __construct(Configuration $config)
28
    {
29 4
        $this->config = $config;
30 4
    }
31
32
    /**
33
     * @param Package $package
34
     * @return bool
35
     */
36 3
    public function make(Package $package): bool
37
    {
38 3
        $fieldsCode = '';
39 3
        $this->package = $package;
40 3
        $fields = $this->package->getFields();
41 3
        foreach ($fields as $field) {
42 3
            $fieldsCode .= $this->generationField($field);
43
        }
44 3
        $code = str_replace([
45 3
            '%FIELDS%',
46
            '%TABLE%',
47
            '%MODEL%',
48
        ], [
49 3
            $fieldsCode,
50 3
            $this->package->getTable(),
51 3
            $this->package->getModel(),
52 3
        ], $this->migrate);
53 3
        $nameFileMigration = date('Y_m_d_His') . '_create_' . $this->package->getTable() . '_table.php';
54 3
        file_put_contents($this->package->getPath('database/migrations/' . $nameFileMigration), $code);
55 3
        return true;
56
    }
57
58
    /**
59
     * @param FieldInterface $field
60
     * @return string
61
     */
62 4
    public function generationField(FieldInterface $field): string
63
    {
64 4
        $code = "\n\t\t\t\$table->" . $field->getType() . '(\'' . $field->getColumn() . '\')';
65 4
        $code .= ($field->isNullable() ? '->nullable()' : '') . ($field->isUnique() ? '->unique()' : '');
66 4
        if (!is_null($field->getReferencesTable())) {
67 1
            $code .= '->references(\'' . $field->getReferencesField() . '\')->on(\'' . $field->getReferencesTable() . '\')';
68
        }
69 4
        if (!is_null($field->getDefault()) || $field->isNullable()) {
70 4
            $default = is_null($field->getDefault()) && $field->isNullable() ? 'null' : "'" . $field->getDefault() . "'";
71 4
            $code .= '->default(' . $default . ')';
72
        }
73 4
        $code .= ';';
74 4
        $code .= $field->isIndex() ? "\n\t\t\t" . '$table->index(\'' . $field->getColumn() . '\');' : '';
75 4
        return $code;
76
    }
77
78
    /**
79
     * PHP code migration
80
     *
81
     * @var string
82
     */
83
    public string $migrate = <<<'EOD'
84
<?php
85
86
declare(strict_types=1);
87
88
use Illuminate\Database\Migrations\Migration;
89
use Illuminate\Database\Schema\Blueprint;
90
use Illuminate\Support\Facades\Schema;
91
92
class Create%MODEL%Table extends Migration
93
{
94
    /**
95
     * Run the migrations.
96
     *
97
     * @return void
98
     */
99
    public function up()
100
    {
101
        Schema::create('%TABLE%', function (Blueprint $table) {
102
            $table->id(); %FIELDS%
103
            $table->timestamps();
104
        });
105
    }
106
107
    /**
108
     * Reverse the migrations.
109
     *
110
     * @return void
111
     */
112
    public function down()
113
    {
114
        Schema::dropIfExists('%TABLE%');
115
    }
116
}
117
EOD;
118
119
}
120