Passed
Push — master ( 4e0c2f...af0465 )
by Prateek
04:42 queued 02:26
created

Seeder::getDataDefinition()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

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

120
                    '{{parent}}'      => /** @scrutinizer ignore-deprecated */ str_singular($parent),

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...
121
                    '{{parentModel}}' => ucfirst(camel_case(str_singular($parent)))
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

121
                    '{{parentModel}}' => ucfirst(camel_case(/** @scrutinizer ignore-deprecated */ str_singular($parent)))

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...
Deprecated Code introduced by
The function camel_case() has been deprecated: Str::camel() 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

121
                    '{{parentModel}}' => ucfirst(/** @scrutinizer ignore-deprecated */ camel_case(str_singular($parent)))

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...
122
                ]);
123
                
124
                if($column != last($columns)) {
125
                    $foreignData .= "," . PHP_EOL;
126
                }
127
            }
128
        }
129
130
        return $foreignData;
131
    }
132
}
133