Passed
Push — master ( 6a3c94...5b674e )
by Prateek
05:01 queued 02:58
created

Seeder::getDataDefinition()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 50
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 40
nc 6
nop 0
dl 0
loc 50
rs 8.9688
c 0
b 0
f 0
1
<?php
2
3
namespace Prateekkarki\Laragen\Generators;
4
5
use Prateekkarki\Laragen\Models\Module;
6
7
class Seeder extends BaseGenerator implements GeneratorInterface
8
{
9
    public function generate()
10
    {
11
        $generatedFiles = [];
12
        $modelTemplate = $this->buildTemplate('Factory', [
13
            '{{modelName}}'      => $this->module->getModelName(),
14
            '{{usedModels}}'     => $this->getUsedModels(),
15
            '{{dataDefinition}}' => $this->getDataDefinition(),
16
            '{{foreignData}}'    => $this->getForeignData()
17
        ]);
18
        $fullFilePath = $this->getPath("database/factories/") . $this->module->getModelName() . "Factory.php";
19
        file_put_contents($fullFilePath, $modelTemplate);
20
        $generatedFiles[] =  $fullFilePath;
21
        
22
        $modelTemplate = $this->buildTemplate('Seeder', [
23
            '{{modelName}}'  => $this->module->getModelName(),
24
            '{{usedModels}}' => $this->getUsedModels()
25
        ]);
26
        $fullFilePath = $this->getPath("database/seeds/") . $this->module->getModelName() . "Seeder.php";
27
        file_put_contents($fullFilePath, $modelTemplate);
28
        $generatedFiles[] =  $fullFilePath;
29
        
30
        return $generatedFiles;         
31
    }
32
33
    protected function getUsedModels(){
34
        $foreignModels = $this->module->getForeignColumns();
35
        $namespace = "App\\Models\\";
36
        $usedModels = "use " . $namespace . $this->module->getModelName() . ";";
37
38
        foreach ($foreignModels as $models) {
39
            foreach($models as $column => $module){
40
                $usedModels .= PHP_EOL . "use " . $namespace . $this->moduleToModelName($module) . ";";
41
            }
42
        }
43
        return $usedModels;
44
    }
45
46
    protected function getDataDefinition(){
47
        $typeToDefinition = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $typeToDefinition is dead and can be removed.
Loading history...
48
49
        $specialTypesToDefinition = [
50
            'title'             => 'title',
51
            'firstname'         => 'firstname',
52
            'lastname'          => 'lastname',
53
            'name'              => 'name',
54
            'company'           => 'company',
55
            'email'             => 'email',
56
            'streetName'        => 'streetName',
57
            'streetAddress'     => 'streetAddress',
58
            'postcode'          => 'postcode',
59
            'address'           => 'address',
60
            'country'           => 'country',
61
            'dateTime'          => 'dateTime',
62
            'month'             => 'month',
63
            'year'              => 'year',
64
            'url'               => 'url',
65
            'slug'              => 'slug',
66
            'short_description' => 'realText(150)',
67
            'long_description'  => 'realText(500)',
68
            'description'       => 'realText(500)',
69
            'content'           => 'realText(500)',
70
        ];
71
72
        $typeToDefinition = [
73
            'string'    => 'sentence',
74
            'int'       => 'randomNumber()',
75
            'text'      => 'realText(500)',
76
            'bool'      => 'numberBetween(0,1)',
77
            'date'      => 'date',
78
            'datetime'  => 'dateTime',
79
        ];
80
81
        $dataDefinition = "";
82
        foreach ($this->module->getNativeData() as $columns) {
83
            foreach($columns as $column => $type){
84
                $specialTypes = array_keys($specialTypesToDefinition);
85
                if(in_array($column,$specialTypes)){
86
                    $dataDefinition .= $this->getTabs(2) . "'{$column}'" . " => " . '$faker->' . $specialTypesToDefinition[$column];
87
                }else{
88
                    $dataDefinition .= $this->getTabs(2) . "'{$column}'" . " => " . '$faker->' . $typeToDefinition[$type];
89
                }
90
91
                if($column != last($columns))
92
                    $dataDefinition .= "," . PHP_EOL;
93
            }
94
        }
95
        return $dataDefinition;
96
    }
97
98
    protected function getForeignData(){
99
        $columns = $this->module->getForeignColumns('parent');
100
101
        $foreignData = "";
102
103
        foreach ($columns as $parents) {
104
            foreach ($parents as $column => $parent) {
105
                $foreignData .= $this->buildTemplate('Factory-parent', [
106
                    '{{parent}}'      => str_singular($parent),
107
                    '{{parentModel}}' => ucfirst(camel_case(str_singular($parent)))
108
                ]);
109
                
110
                if($column != last($columns))
111
                    $foreignData .= "," . PHP_EOL;
112
            }
113
        }
114
115
        return $foreignData;
116
    }
117
}
118