Passed
Push — main ( dcb144...5c7fda )
by Garbuz
03:59
created

Migration   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 67
c 1
b 0
f 0
dl 0
loc 75
ccs 31
cts 31
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A make() 0 20 2
B generationField() 0 16 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 3
    public function __construct(Configuration $config)
28
    {
29 3
        $this->config = $config;
30 3
    }
31
32
    /**
33
     * @param Package $package
34
     * @return bool
35
     */
36 2
    public function make(Package $package): bool
37
    {
38 2
        $fieldsCode = '';
39 2
        $this->package = $package;
40 2
        $fields = $this->package->getFields();
41 2
        foreach ($fields as $field) {
42 2
            $fieldsCode .= $this->generationField($field);
43
        }
44 2
        $code = str_replace([
45 2
            '%FIELDS%',
46
            '%TABLE%',
47
            '%MODEL%',
48
        ], [
49 2
            $fieldsCode,
50 2
            $this->package->getTable(),
51 2
            $this->package->getModel(),
52 2
        ], $this->migrate);
53 2
        $nameFileMigration = date('Y_m_d_His') . '_create_' . $this->package->getTable() . '_table.php';
54 2
        file_put_contents($this->package->getPath('migrations/' . $nameFileMigration), $code);
55 2
        return true;
56
    }
57
58
    /**
59
     * @param FieldInterface $field
60
     * @return string
61
     */
62 3
    public function generationField(FieldInterface $field): string
63
    {
64 3
        $code = "\n\t\t\t";
65 3
        $code .= '$table->' . $field->getType() . '(\'' . $field->getColumn() . '\')';
66 3
        $code .= $field->isNullable() ? '->nullable()' : '';
67 3
        $code .= $field->isUnique() ? '->unique()' : '';
68 3
        if (!is_null($field->getReferencesTable())) {
69 1
            $code .= '->references(\'' . $field->getReferencesField() . '\')->on(\'' . $field->getReferencesTable() . '\')';
70
        }
71 3
        if (!is_null($field->getDefault()) || $field->isNullable()) {
72 3
            $default = is_null($field->getDefault()) && $field->isNullable() ? 'null' : "'" . $field->getDefault() . "'";
73 3
            $code .= '->default(' . $default . ')';
74
        }
75 3
        $code .= ';';
76 3
        $code .= $field->isIndex() ? "\n\t\t\t" . '$table->index(\'' . $field->getColumn() . '\');' : '';
77 3
        return $code;
78
    }
79
80
    /**
81
     * PHP code migration
82
     *
83
     * @var string
84
     */
85
    public string $migrate = <<<'EOD'
86
<?php
87
88
declare(strict_types=1);
89
90
use Illuminate\Database\Migrations\Migration;
91
use Illuminate\Database\Schema\Blueprint;
92
use Illuminate\Support\Facades\Schema;
93
94
class Create%MODEL%Table extends Migration
95
{
96
    /**
97
     * Run the migrations.
98
     *
99
     * @return void
100
     */
101
    public function up()
102
    {
103
        Schema::create('%TABLE%', function (Blueprint $table) {
104
            $table->id(); %FIELDS%
105
            $table->timestamps();
106
        });
107
    }
108
109
    /**
110
     * Reverse the migrations.
111
     *
112
     * @return void
113
     */
114
    public function down()
115
    {
116
        Schema::dropIfExists('%TABLE%');
117
    }
118
}
119
EOD;
120
121
}
122