Completed
Push — master ( 4d49f2...b9481e )
by Prateek
05:48 queued 03:41
created

Migration   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchema() 0 17 3
A generate() 0 13 1
1
<?php
2
3
namespace Prateekkarki\Laragen\Generators;
4
use Prateekkarki\Laragen\Models\Module;
5
use Prateekkarki\Laragen\Models\DataOption;
6
7
class Migration extends BaseGenerator implements GeneratorInterface
8
{
9
    protected static $counter = 0;
10
11
    public function generate(Module $module)
12
    {
13
        $this->setModule($module);
14
15
        $migrationTemplate = $this->buildTemplate('Migration', [
16
            '{{modelName}}'         => $module->getModelName(),
17
            '{{modelNamePlural}}'   => $module->getModelNamePlural(),
18
            '{{moduleName}}'        => $module->getModuleName(),
19
            '{{modelTableSchema}}'  => $this->getSchema()
20
        ]);
21
        
22
        $dateSuffix = (int)date('His') + ++self::$counter;
23
        file_put_contents(database_path() . "/migrations/" . date('Y_m_d_') . $dateSuffix . "_create_" . $module->getModuleName() . "_table.php", $migrationTemplate);
24
    }
25
26
    protected function getSchema()
27
    {
28
        $schema = "";
29
        $keyArray = array_keys($this->module->getData());
30
        $lastColumn = array_pop($keyArray);
31
32
        foreach ($this->module->getData() as $column => $optionString) {
33
            $option = new DataOption($column, $optionString);
34
35
            $schema .= $option->getSchema();
36
37
            if ($column != $lastColumn) {
38
                $schema .= PHP_EOL . $this->getTabs(3);
39
            }
40
        }
41
42
        return $schema;
43
    }
44
}
45