Seed   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 47
c 1
b 0
f 0
dl 0
loc 57
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A make() 0 24 3
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
10
class Seed
11
{
12
    /**
13
     * @var Configuration
14
     */
15
    private Configuration $config;
16
17
    /**
18
     * @var Package
19
     */
20
    private Package $package;
21
22
    /**
23
     * FileGenerator constructor.
24
     * @param Configuration $config
25
     */
26 3
    public function __construct(Configuration $config)
27
    {
28 3
        $this->config = $config;
29 3
    }
30
31
    /**
32
     * @param Package $package
33
     * @return bool
34
     */
35 3
    public function make(Package $package): bool
36
    {
37 3
        $this->package = $package;
38
39 3
        if (!$this->package->getGeneratorSeed()) {
40 1
            return false;
41
        }
42
43 2
        $fields = '';
44 2
        $fieldsAll = $this->package->getFields();
45 2
        foreach ($fieldsAll as $field) {
46 2
            $fields .= "\n\t\t\t'" . $field->getColumn() . "' => '',";
47
        }
48 2
        $code = str_replace([
49 2
            '%PACKAGE_NAMESPACE%',
50
            '%MODEL%',
51
            '%FIELDS%',
52
        ], [
53 2
            $this->package->getNamespace(),
54 2
            $this->package->getModel(),
55 2
            $fields,
56 2
        ], $this->template);
57 2
        file_put_contents($this->package->getPath('database/Seeders/DatabaseSeeder.php'), $code);
58 2
        return true;
59
    }
60
61
    /**
62
     * PHP code model
63
     *
64
     * @var string
65
     */
66
    public string $template = <<<'EOD'
67
<?php
68
69
declare(strict_types=1);
70
71
namespace %PACKAGE_NAMESPACE%\DataBase\Seeders;
72
73
use Illuminate\Database\Seeder;
74
use %PACKAGE_NAMESPACE%\Models\%MODEL%;
75
76
class DatabaseSeeder extends Seeder
77
{
78
    /**
79
     * Seed the application's database.
80
     *
81
     * @return void
82
     */
83
    public function run()
84
    {
85
        %MODEL%::create([ %FIELDS%
86
        ]);
87
88
        %MODEL%::factory(10)->create();
89
    }
90
}
91
EOD;
92
93
}
94