Passed
Push — main ( 6e7d22...1eca71 )
by Garbuz
03:46
created

Migration::generationField()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 10
cc 4
nc 8
nop 1
crap 20
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 2
     */
27
    public function __construct(Configuration $config)
28 2
    {
29 2
        $this->config = $config;
30
    }
31
32
    /**
33
     * @param Package $package
34
     * @return bool
35 2
     */
36
    public function make(Package $package): bool
37 2
    {
38 2
        $fieldsCode = '';
39 2
        $this->package = $package;
40 2
        $fields = $this->package->getFields();
41
        foreach ($fields as $field) {
42
            $fieldsCode .= $this->generationField($field);
43 2
        }
44 2
        $code = str_replace([
45
            '%FIELDS%',
46
            '%TABLE%',
47
            '%MODEL%',
48 2
        ], [
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
        return true;
56
    }
57
58
    /**
59
     * @param FieldInterface $field
60
     * @return string
61
     */
62
    public function generationField(FieldInterface $field): string
63
    {
64
        $code = '$table->' . $field->getType() . '(\'' . $field->getColumn() . '\')';
0 ignored issues
show
Bug introduced by
The method getColumn() does not exist on GarbuzIvan\LaravelGenera...ontracts\FieldInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to GarbuzIvan\LaravelGenera...ontracts\FieldInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $code = '$table->' . $field->getType() . '(\'' . $field->/** @scrutinizer ignore-call */ getColumn() . '\')';
Loading history...
Bug introduced by
The method getType() does not exist on GarbuzIvan\LaravelGenera...ontracts\FieldInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to GarbuzIvan\LaravelGenera...ontracts\FieldInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $code = '$table->' . $field->/** @scrutinizer ignore-call */ getType() . '(\'' . $field->getColumn() . '\')';
Loading history...
65
        $code .= $field->getNullable() ? '->nullable()' : '';
0 ignored issues
show
Bug introduced by
The method getNullable() does not exist on GarbuzIvan\LaravelGenera...ontracts\FieldInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to GarbuzIvan\LaravelGenera...ontracts\FieldInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $code .= $field->/** @scrutinizer ignore-call */ getNullable() ? '->nullable()' : '';
Loading history...
66
        $code .= $field->getUnique() ? '->unique()' : '';
0 ignored issues
show
Bug introduced by
The method getUnique() does not exist on GarbuzIvan\LaravelGenera...ontracts\FieldInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to GarbuzIvan\LaravelGenera...ontracts\FieldInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $code .= $field->/** @scrutinizer ignore-call */ getUnique() ? '->unique()' : '';
Loading history...
67
        if (!is_null($field->getReferencesTable())) {
0 ignored issues
show
Bug introduced by
The method getReferencesTable() does not exist on GarbuzIvan\LaravelGenera...ontracts\FieldInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to GarbuzIvan\LaravelGenera...ontracts\FieldInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        if (!is_null($field->/** @scrutinizer ignore-call */ getReferencesTable())) {
Loading history...
68
            $code .= '->references(\'' . $field->getReferencesField() . '\')->on(\'' . $field->getReferencesTable() . '\')';
0 ignored issues
show
Bug introduced by
The method getReferencesField() does not exist on GarbuzIvan\LaravelGenera...ontracts\FieldInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to GarbuzIvan\LaravelGenera...ontracts\FieldInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            $code .= '->references(\'' . $field->/** @scrutinizer ignore-call */ getReferencesField() . '\')->on(\'' . $field->getReferencesTable() . '\')';
Loading history...
69
        }
70
        return "\n\t\t\t" . $code . ";";
71
    }
72
73
    /**
74
     * PHP code migration
75
     *
76
     * @var string
77
     */
78
    public string $migrate = <<<'EOD'
79
<?php
80
81
declare(strict_types=1);
82
83
use Illuminate\Database\Migrations\Migration;
84
use Illuminate\Database\Schema\Blueprint;
85
use Illuminate\Support\Facades\Schema;
86
87
class Create%MODEL%Table extends Migration
88
{
89
    /**
90
     * Run the migrations.
91
     *
92
     * @return void
93
     */
94
    public function up()
95
    {
96
        Schema::create('%TABLE%', function (Blueprint $table) {
97
            $table->id(); %FIELDS%
98
            $table->timestamps();
99
        });
100
    }
101
102
    /**
103
     * Reverse the migrations.
104
     *
105
     * @return void
106
     */
107
    public function down()
108
    {
109
        Schema::dropIfExists('%TABLE%');
110
    }
111
}
112
EOD;
113
114
}
115