Passed
Push — master ( 9c82b2...472045 )
by Prateek
07:54 queued 04:44
created

Migration::getMultipleSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Common;
3
4
use Prateekkarki\Laragen\Generators\BaseGenerator;
5
use Prateekkarki\Laragen\Generators\GeneratorInterface;
6
use Prateekkarki\Laragen\Models\DataOption;
7
8
class Migration extends BaseGenerator implements GeneratorInterface
9
{
10
    protected static $counter = 0;
11
12
    public function generate()
13
    {
14
        $generatedFiles = [];
15
        $migrationTemplate = $this->buildTemplate('common/migrations/table', [
16
            '{{modelName}}'         => $this->module->getModelName(),
17
            '{{modelNamePlural}}'   => $this->module->getModelNamePlural(),
18
            '{{moduleName}}'        => $this->module->getModuleName(),
19
            '{{modelTableSchema}}'  => $this->getSchema()
20
        ]);
21
        
22
        $fullFilePath = $this->getMigrationFile();
23
        file_put_contents($fullFilePath, $migrationTemplate);
24
        $generatedFiles[] = $fullFilePath;
25
26
        foreach ($this->module->getGalleries() as $gallery) {
27
            $pivotTemplate = $this->buildTemplate('common/migrations/pivot', [
28
                '{{pivotName}}'         => $this->module->getPivotName($gallery),
29
                '{{pivotTableName}}'    => $this->module->getPivotTableName($gallery),
30
                '{{pivotTableSchema}}'  => $this->getGallerySchema($gallery)
31
            ]);
32
            $pivotFilePath = $this->getPivotFile($gallery);
33
            file_put_contents($pivotFilePath, $pivotTemplate);
34
            $generatedFiles[] = $pivotFilePath;
35
        }
36
37
        foreach ($this->module->getForeignColumns('related') as $relatedModules) {
38
            foreach ($relatedModules as $related) {
39
                $pivotTemplate = $this->buildTemplate('common/migrations/pivot', [
40
                    '{{pivotName}}'         => $this->module->getPivotName($related),
41
                    '{{pivotTableName}}'    => $this->module->getPivotTableName($related),
42
                    '{{pivotTableSchema}}'  => $this->getPivotSchema($related)
43
                ]);
44
            }
45
            $pivotFilePath = $this->getPivotFile($related);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $related does not seem to be defined for all execution paths leading up to this point.
Loading history...
46
            file_put_contents($pivotFilePath, $pivotTemplate);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pivotTemplate does not seem to be defined for all execution paths leading up to this point.
Loading history...
47
            $generatedFiles[] = $pivotFilePath;
48
        }
49
50
        foreach ($this->module->getMultipleColumns() as $multipleModules) {
51
            foreach ($multipleModules as $multiple => $multipleData) {
52
                $pivotTemplate = $this->buildTemplate('common/migrations/pivot', [
53
                    '{{pivotName}}'         => str_singular($this->module->getPivotName($multiple)),
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

53
                    '{{pivotName}}'         => /** @scrutinizer ignore-deprecated */ str_singular($this->module->getPivotName($multiple)),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
54
                    '{{pivotTableName}}'    => str_plural($this->module->getPivotTableName($multiple)),
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

54
                    '{{pivotTableName}}'    => /** @scrutinizer ignore-deprecated */ str_plural($this->module->getPivotTableName($multiple)),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
55
                    '{{pivotTableSchema}}'  => $this->getMultipleSchema($multipleData)
56
                ]);
57
                
58
                $pivotFilePath = $this->getPivotFile($multiple);
59
                file_put_contents($pivotFilePath, $pivotTemplate);
60
                $generatedFiles[] = $pivotFilePath;
61
            }
62
        }
63
64
        return $generatedFiles;
65
    }
66
67
    protected function getMigrationFile()
68
    {
69
        $fileCounter = sprintf('%06d', (int) date('His') + ++self::$counter);
70
        $filenamePrefix = date('Y_m_d_').$fileCounter."_";
71
        $fileName = "create_".$this->module->getModuleName()."_table.php";
72
73
        $existingFiles = scandir($this->getPath("database/migrations/"));
74
        
75
        foreach ($existingFiles as $file) {
76
            if (strpos($file, $fileName) !== false) {
77
                $filenamePrefix = str_replace($fileName, "", $file);
78
            }
79
        }
80
        return $this->getPath("database/migrations/").$filenamePrefix.$fileName;
81
    }
82
83
    protected function getPivotFile($related)
84
    {
85
        $fileCounter = sprintf('%06d', (int) date('His') + ++self::$counter);
86
        $filenamePrefix = date('Y_m_d_').$fileCounter."_";
87
        $fileName = "create_".str_singular($this->module->getPivotTableName($related))."_table.php";
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

87
        $fileName = "create_"./** @scrutinizer ignore-deprecated */ str_singular($this->module->getPivotTableName($related))."_table.php";

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
88
89
        $existingFiles = scandir($this->getPath("database/migrations/"));
90
        
91
        foreach ($existingFiles as $file) {
92
            if (strpos($file, $fileName) !== false) {
93
                $filenamePrefix = str_replace($fileName, "", $file);
94
            }
95
        }
96
        
97
        return $this->getPath("database/migrations/").$filenamePrefix.$fileName;
98
    }
99
100
    protected function getMultipleSchema($multipleData)
101
    {
102
        $schema =  '$table->bigInteger("'.$this->module->getModelNameLowercase().'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3);
103
        $schema .= "\$table->foreign('".$this->module->getModelNameLowercase()."_id')->references('id')->on('".$this->module->getModulename()."')->onDelete('set null');";
104
105
        foreach($multipleData as $column => $optionString){
106
            $option = new DataOption($column, $optionString);
107
            $schema .= $option->getSchema();
108
            $schema .=  ''.PHP_EOL.$this->getTabs(3);
109
        }
110
        return $schema;
111
    }
112
113
    protected function getPivotSchema($related)
114
    {
115
        $schema =  '$table->bigInteger("'.$this->module->getModelNameLowercase().'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3);
116
        $schema .= "\$table->foreign('".$this->module->getModelNameLowercase()."_id')->references('id')->on('".$this->module->getModulename()."')->onDelete('set null');";
117
118
        $schema .= '$table->bigInteger("'.str_singular($related).'_id")->unsigned()->nullable();';
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

118
        $schema .= '$table->bigInteger("'./** @scrutinizer ignore-deprecated */ str_singular($related).'_id")->unsigned()->nullable();';

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
        $schema .= "\$table->foreign('".str_singular($related)."_id')->references('id')->on('".$related."')->onDelete('set null');";
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

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

119
        $schema .= "\$table->foreign('"./** @scrutinizer ignore-deprecated */ str_singular($related)."_id')->references('id')->on('".$related."')->onDelete('set null');";

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
121
        return $schema;
122
    }
123
124
    protected function getGallerySchema($gallery)
0 ignored issues
show
Unused Code introduced by
The parameter $gallery is not used and could be removed. ( Ignorable by Annotation )

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

124
    protected function getGallerySchema(/** @scrutinizer ignore-unused */ $gallery)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        $schema =  '$table->bigInteger("'.$this->module->getModelNameLowercase().'_id")->unsigned()->nullable();'.PHP_EOL.$this->getTabs(3);
127
        $schema .= "\$table->foreign('".$this->module->getModelNameLowercase()."_id')->references('id')->on('".$this->module->getModulename()."')->onDelete('set null');";
128
        $schema .= '$table->string("filename", 128);';
129
        $schema .= '$table->timestamps();';
130
        return $schema;
131
    }
132
133
    protected function getSchema()
134
    {
135
        $schema = "";
136
        $keyArray = array_keys($this->module->getData());
137
        $lastColumn = array_pop($keyArray);
138
139
        foreach ($this->module->getData() as $column => $optionString) {
140
            $option = new DataOption($column, $optionString);
141
142
            $schema .= $option->getSchema();
143
144
            if ($column != $lastColumn) {
145
                $schema .= PHP_EOL.$this->getTabs(3);
146
            }
147
        }
148
149
        return $schema;
150
    }
151
}
152